函数
目录
1. 字符串函数
ASCII(s) 返回字符串 s 的第一个字符的 ASCII 码。
length() 返回字符串长度
concat (s1,s2...sn) 字符串 s1,s2 等多个字符串合并为一个字符串
locate(s1,s) s1在s中的位置
lcase(s) 把s转换小写
ucase(s) 把s转换为大写
left(s,n) 从左返回指定字符串的 n个字符
right(s,n) 从右返回指定字符串的 n个字符
ltrim() rtrim() 去掉左 , 右两边空格
replace(s,s1,s2) s中的s1用s2替代
reverse(s) 反向输出字符串
strcmp(s1,s2) 比较字符串大小 相等0 s1大1 小-1
substr(s,begin,length) 截取字符串
2.数字函数
abs() 绝对值
MOD(x,y) 返回余数
PI() 圆周率
pow(x,y) x的y次方
rand() 返回 0 到 1 的随机数
ceil() 向上取整
floor() 向下取整
round() 四舍五入
3. 日期函数
curdate() 返回当前日期 年月日
curtime() 返回当前时间 时分秒
now() 当前年月日时分秒
date() year() month() day(d) WEEK(d)第几个星期 WEEKDAY(d) 星期几从0开始
datediff(d1,d2) 计算日期 d1->d2 之间相隔的天数
timediff(time1, time2) 时间差值
date_add(d,interval 值 type) type: hour second minute day 等 计算添加指定的时间
date_format(d,f) 按f格式化日期 select DATE_FORMAT(now(),'%Y-%m-%d %h:%i:%s')
DATE_SUB(date,INTERVAL expr type)
timestampdiff(DAY,'2003-02-01','2003-05-01'); // 计算两个时间相隔多少天
str_to_date(string, format_mask) select STR_TO_DATE('1999-09-08','%Y-%m-%d')
4. 高级函数
IF(expr,v1,v2) 表达式 expr 成立,返回结果 v1;否则,返回结果 v2。
IFNULL(v1,v2) v1 的值不为 NULL,则返回 v1,否则返回 v2 IFNULL(comm,'没有佣金')
case when score < 60 then '不及格'
when score >=60 and score <=70 then '及格'
when score >70 and score <= 80 then '良好'
else '优秀' end
case cname when 'red' then '红色'
when 'green' then '绿色'
when 'blue' then '蓝色'
else '不认识' end
5. 聚合函数
1. avg() | sum()
2. max() | min()
3. count()
查询一个表中有多少行
方式一: count(*)
方式二: count(1)
方式三: count(字段名) //不包含空值的,不计算null
count(*) null
count(1) 和 count(字段) 不计算null