mysql语法

0. 登陆mysql

mysql -u root -p
mysql -h 192.168.9.168 -u root -p #远程登陆

http://unix-cd.com/unixcd12/article_6728.html    MySQL常用命令一(登录、增加用户、密码更改)

1.没有print,要输出只能select

SELECT 'Hello World'

2. 循环和MSSQL 不一样,要create procedure

先create procedure

CREATE PROCEDURE dowhile6()
BEGIN
  DECLARE i INT DEFAULT 5;

  WHILE i > 0 DO

    SET i = i - 1;
    select 'hello';

  END WHILE;
END;

然后run 

call dowhile6()

 3. 让select查询添加自增序列。在MSSQL中可以用函数:ROW_NUMBER(),但是mysql没有

http://blog.163.com/hyuyu20@126/blog/static/116210145201110214010934/  SQL Server 2005通过select添加一个序号列

http://blog.csdn.net/ijong/article/details/7068265 MySQL用变量的方法添加伪序号列,MySql自增序列,序号列

SELECT @rownum:=@rownum+1 AS rownum, table_name.* FROM (SELECT @rownum:=0) r, table_name ;

4. update

mysql的update语句不能用from

在sql server中可以这么写

#逐行更新
update b 
set num=a.num
from a
where a.id=b.id

在mysql中不能有from,用到的表都写在update后

update b,a
set b.num=a.num
where a.id=b.id

 5. limit语句用于返回限定的行

select * from users limit 100,200 #返回100-200行

http://www.phpweblog.net/peiyinjin/archive/2008/04/15/3199.html      mysql中limit的用法详解[数据分页常用]

在MSSQL中用top

select top N * from table

6.err: Every derived table must have its own alias

原因:MYSQL要求派生表要有别名

例如

select * from 
(select uid, name, f_message, message from tag_all) as a # 不写as就错了
where a.uid<101

 7. 查看mysql版本

http://www.yayu.org/look.php?id=113  查看mysql版本的四种方法

select version()

或:

mysql -V  

或:

status      

8. delimiter 分隔符,告诉解释器该段命令已经结束了

http://www.cnblogs.com/rootq/archive/2009/05/27/1490523.html      MySql中delimiter的作用是什么?

9. 创建mysql函数(存储过程)

delimiter //
create function 函数名(参数名 参数类型)returns 返回类型 begin statement; end //

     示例如下:

     create function gets(s int)returns int
          begin
               declare a int;     //mysql中声明变量不需要@!!!跟其他数据库不太一样!!切记!!!
               set @a=3;       //每句之后需加一个;不然报错!!!
               return @a+s;   //要有返回语句!!!

          end;

10. 把查询结果放入新表中

create table temp_users as
select * from users

http://space.itpub.net/9524377/viewspace-660041  最好不要这么创建表。原因是这样只创建表的结构,而不会将原表的默认值一起创建。

还有select into from 和 insert into select 。但是mysql 不支持select into

http://www.cnblogs.com/netsa/archive/2011/10/26/2225625.html 

select * into temp_users from users #mysql不支持

http://blog.sina.com.cn/s/blog_5623b16b0100hlgc.html  MySQL select into 用法

11. order by rand() 随即顺序

select * from users order by rand()

12. auto_increment 自增加

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 

 插入式通过last_insert_id()获取id值来插入

13. explain select 对select语句解析

explain select * from users where name='米恩文化'

14.  拼接字段  concat(),将多个字段拼接成一个字符串

select concat (name,' (', location,' )') from z_users

15. 函数

(1)文本处理函数 

字符串截取:substring(str, pos); substring(str, pos, len)

RTRIM(str) 返回删除了其拖后空格字符的字符串str。

http://justdo2008.iteye.com/blog/1141609   MySQL字符串函数:字符串截取

(2)日期时间函数

select now()

http://www.cnblogs.com/she27/articles/1377089.html  MySQL:日期函数、时间函数总结(MySQL 5.X)

http://blog.csdn.net/deyinchan/article/details/3031447   mysql日期时间函数(转载)特别详细

http://www.webasp.net/article/25/24538.htm  Mysql日期和时间函数不求人

(3) 数值处理函数

exp()

mod()

pi()

rand()

sqrt()

16. 错误:Every derived table must have its own alias

MYSQL要求派生表要有别名

http://hi.baidu.com/guoliang24/item/2ca30c16db2de921f6625c8c

select * from
(
select a.to_user as uid from tag_follows as a,tag_users as b
where b.tag=1 and a.from_user=b.id  
) as t  #这里t必须写

17. 查看数据库存储位置

show variables like '%datadir%'

 

18.join 表连接

http://www.jb51.net/article/15386.htm    超详细mysql left join,right join,inner join用法分析

http://www.oschina.net/question/89964_65912   关于 MySQL LEFT JOIN 你可能需要了解的三点  (ON 子句和 WHERE 子句有什么不同?

left join 左边的表全部显示,右边的表如无对应则空缺 NULL。right join同理。

SELECT * FROM a 
LEFT JOIN b 
ON a.aID =b.bID 

结果如下: 
aID aNum bID bName 
1 a20050111 1 2006032401 
2 a20050112 2 2006032402 
3 a20050113 3 2006032403 
4 a20050114 4 2006032404 
5 a20050115 NULL NULL 
(所影响的行数为 5 行)

19。 字符串连接 concat   concat_ws

http://database.51cto.com/art/201005/202171.htm MySQL字符串如何正确连接函数

http://hi.baidu.com/leeity/item/e7d1a3360b8a36d06d15e93f  mysql字符串连接函数

concat()函数连接字符串时如果有一个字符串是null,返回的结果就是null。

concat_ws()函数可以解决这个问题

CONCAT_WS(separator,str1,str2,...)

select concat_ws(',','11','22','33') #前面逗号一定要写

20  Mysql error 1093 - Can't specify target table for update in FROM clause

http://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause

update 语句的子句不能是要update的table。

解决方法:再加一层嵌套。

update tb  #错误代码
set c=null
where id in 
(select id from tb
) as t
update tb #正确代码
set c=null
where id in 
(select * from 
(select id from tb
) as t
)
原文地址:https://www.cnblogs.com/phoenix13suns/p/2815312.html