一、 数学函数
1、语法: round()
# rount(x)
select round(1.49) //x小数位小于0.5, 取值为1
select round(1.5) //x小数位大于0.5, 四舍五入取值为2
# rount(x,y)
select round(1.78123,3) //取x的y位小数, 取值为1.781
2、语法: ceil()
向上取整函数
# ceil(x)
select ceil(1.001) //只要有小数位, x+1, 取值为2
3、语法: floor()
向下取整函数
# floor(x)
select floor(1.01) //取x的整数位, 取值为1
select floor(-1.01) //取值为-2
4、语法: truncate()
截取函数
# truncate(x,y)
select truncate(1.33339,2) //y为截取的小数位, 取值为1.33
5、语法: mod()
取余函数
# mod(x,y)
select mod(10,3) //返回x/y的余数, 余数为1
6、语法: greatest()
返回集合中最大的值函数
# greatest(value1,value2,...)
select greatest(-1,99,96,-33) //最大值99
7、语法: least()
返回集合中最小的值函数
# least(value1,value2,...)
select least(-1,99,96,-33) //最小值-33
二、字符函数
1、语法: lcase (str)
将文本转换成小写
# lcase(str)
select lcase('XIAO PAN') //转换成小写, xiao pan
2、语法: ucase (str)
将文本转换成大写
# ucase(str)
select ucase('xiao pan') //转换成大写, XIAO PAN
3、语法: trim ()
分别去掉两头的空格
# trim()
select trim(' 部门经理 ') //部门经理
4、语法: repeat ()
重复值n次
# repeat(str,count)
select repeat('hello world ',3) //重复str count次, 值为hello world hello world hello world
5、语法: reverse ()
颠倒值
# reverse(str)
select reverse('hello world') //颠倒str, 值为dlrow olleh
6、语法: strcmp()
比较两个字符串
# strcmp(expr1,expr2)
select strcmp('hello','hello') //比较两个字符串,一模一样返回0,不一样返回-1
7、语法: length(username) < 6
字符串长度函数
select * from user where length(username) < 6 //查出用户名长度小于6个字符的用户列表
8、语法: locate(字符,字段)
某个字段包含字符串
select * from wx_appraise where locate('JQ', pid);
9. 语法: FIND_IN_SET(str,strlist)
第一个参数:可以是字段也可以是字段中的一个数据
第二个参数:可以使字段集合也可以是以逗号作为分割根据的字符串
三、 日期函数
1、语法:now ()
返回当前日期+时间
select now() //返回当前日期+时间, 如: 2022-01-25 13:09:58
2、语法: curdate()
返回当前日期,不含时间
select curdate() //如: 2022-01-25
3、语法: date_add (date,INTERVAL expr unit)
返回加上年月日或时分秒的时间
# 当前日期为: 2022-01-35、返回date加上int日期后的日期, 年月日
select date_add(curdate(),interval 5 year) //2027-01-25
select date_add(curdate(),interval 5 month) //2022-06-25
select date_add(curdate(),interval 5 day) //2022-01-30
# 当前时间为: 13:21:20、返回date加上int时间后的时间, 时分秒
select date_add(curtime(),interval 5 hour) //18:21:26
select date_add(curtime(),interval 5 minute) //13:26:37
select date_add(curtime(),interval 5 second) //13:21:50
4、语法: date_format (指定时间格式)
将日期转换成字符
当前时间:
DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s')
select date_format(now(),'%Y-%m-%d') //2022-01-25
select date_format(now(),'%Y年%m月%d日') //2022年1月25日
5、语法: str_to_date ()
将日期格式的字符转换成日期
select str_to_date('1996-3-9','%Y-%m-%d') //1996-03-09
select str_to_date('6-3 1991','%m-%d %Y') //1991-06-03
6、year()、month()、day()、hour()、minute()、second()…等函数可以获取指定的部分,年、月、日、时、分、 秒
// 2022-01-25 13:35:23
select year(now()) //2022年
select month(now()) //1月
select day(now()) //25日
select hour(now()) //13时
select minute(now()) //35分
select second(now()) //23秒
7、语法: dayname()
返回星期几
# 星期一: Monday 星期二: Tuesday 星期三: Wednesday 星期四: Thursday 星期五: Friday 星期六: Saturday 星期天: Sunday
select dayname(curdate());
8、语法: monthname()
返回月份
# 一月:January 二月:February 三月:March 四月:April 五月:May 六月:June 七月:July 八月:August 九月:September十月:October十一月:November 十二月:December
select monthname(curdate());
9、语法: timestampdiff()
返回 计算两个时间差
select timestampdiff(year,19920202,curdate())
10. 语法: DATEDIFF(createTime, now()) = 0
查询时间是当天的记录
返回两个日期之间的天数,所以返回为0就是同一天
四、 聚合函数
1、语法: avg(col)
返回指定列平均值
# 查询所有商品平均零售价
select avg(salePrice)from product
2、语法: count(col)
返回指定列中非null总数
# 查询商品总记录数(注意在Java7前必须使用long接收)
select count(id) from product
3、语法: max(col)
返回指定列最大值
# 查询商品的最高零售价
select max(salePrice) from product
4、语法: min(col)
返回指定列最小值
# 查询商品的最低零售价
select min(salePrice) from product
5、语法: sum(col)
返回指定列的总和
# 查询所有商品零售价总和
select sum(salePrice) from product
五、 判断函数
1、语法: if()
select if(10>7,'大','小') //大
2、语法: switch()
# case 相当于java中 switch case
select salary
case
when salary > 2000 then 'a'
when salary > 1500 then 'b'
when salary > 1000 then 'c'
else 'd'
end
from pople