phoenix mysql函数_mysql语法

本文介绍了MySQL的一些使用技巧和注意事项,包括登陆、输出、循环创建、查询自增序列、UPDATE语句、LIMIT使用、派生表别名、创建函数、ORDER BY RAND()、AUTO_INCREMENT、EXPLAIN SELECT、字段拼接、数值处理函数以及JOIN操作等。同时,文章也提到了在MySQL中遇到的一些错误及其解决方案。
摘要由CSDN通过智能技术生成

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()

c6c7a833cec70da6a060cefec3a4e064.png

或:

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='米恩文化'

57386468649a7b39ba7a53de1e922073.png

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

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值