数值函数
select abs(-3.14);--绝对值,3.14
select format(3.14,1);--格式化小数,3.1
select ceil(3.14);--向上取整,4
select floor(3.6);--向下取整,3
select round(3.14);--四舍五入取整,3
select mod(10,3);--取余,1
select pi();--圆周率
select pow(2,3);--2的3次方,8
select sqrt(16);--算法平方根,4
select rand();--0到1之间的随机数
select truncate(3.1415,3);--截取3位小数
日期时间函数
select now();--获取当前时间,2017-03-19 16:30:16
select current_timestamp();--获取当前时间,2017-03-19 16:30:50
select current_date();--获取当前日期,2017-03-19
select current_time();--获取当前时间,16:31:33
select date(now());--获取日期部分,2017-03-19
select time(now());--获取时间部分,16:34:53
select date_format(now(),'%Y/%m/%d');--格式化时间,2017/03/19
select unix_timestamp();--获取unix时间戳,1489912619
select from_unixtime(1489912619);--时间戳转时间,2017-03-19 16:36:59
字符串函数
select length('杭州');--几个字节长度,6
select char_length('杭州');--几个字符个数,2
select substring('abcdef',1,2);--字符串截取,ab
select replace('abcabc','ab','AB');--字符串替换,ABcABc
select instr('abcabc','b');--字符第一次出现索引位置,2
select locate('b','abcabc',3);--字符串从3开始出现索引位置,5
select concat('aa','bb','cc');--字符串连接,aabbcc
select charset('abc');--字符字符集,utf8
select lcase('ABC');--转换成小写,abc
select ucase('abc');--转换成大写,ABC
select left('abcdef',2);--从左边起取字符,ab
select right('abcdefe',2);--从右边起取字符,fe
select lpad('abc',8,'0');--从左开始填充,00000abc
select rpad('abc',8,'0');--从右开始填充,abc00000
--select load_file('D:\aa.txt');--读文件,要权限
select ltrim(' abc');--去左空格
select rtrim('abc ');--去右空格
select repeat('abc',2);--字符串重复,abcabc
select strcmp('abc','abd');--字符串比较大小
聚合函数
select count(1) from user;
select sum(age) from user;
select max(age) from user;
select min(age) from user;
select avg(age) from user;
case...when...
select
case when username='1' then '用户1'
when username='2' then '用户2'
else '用户3' end
from user_accounts;
变量赋值
-- 方式一:
select @num:=count(1) from t_company;
-- 方式二:
set @num = 314;
select @num;
-- 方式三:
set @num := 1;
select @num;