0. 登陆mysql
mysql -u root -p
mysql -h 192.168.9.168 -u root -p #远程登陆
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没有
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行
在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版本
select version()
或:
mysql -V
或:
status
8. 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
还有select into from 和 insert into select 。但是mysql 不支持select into
select * into temp_users from users #mysql不支持
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。
(2)日期时间函数
select now()
(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 asbwhere 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 * FROMaLEFT JOINbON 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.htmMySQL字符串如何正确连接函数
http://hi.baidu.com/leeity/item/e7d1a3360b8a36d06d15e93f mysql字符串连接函数
concat()函数连接字符串时如果有一个字符串是null,返回的结果就是null。
concat_ws()函数可以解决这个问题
CONCAT_WS(separator,str1,str2,...)
select concat_ws(',','11','22','33') #前面逗号一定要写
update 语句的子句不能是要update的table。
解决方法:再加一层嵌套。
update tb #错误代码
set c=null
where idin(select idfromtb
) as t
update tb #正确代码
set c=null
where idin(select* from(select idfromtb
) as t
)