MySQL知识要点6--函数总结

函数分类

MySQL中的函数大致可以分为单行函数聚合函数分组函数加密函数

单行函数

单行函数主要包括:数学函数、字符函数、日期函数

数学函数
	select pi(); # pi
	select ceil(-12.3); # -12 向上(大)取整
	select floor(-12.3); # -13 向下取整
	select round(4.5); # 5 四舍五入
	select mod(5,2); # 1 取模
	select rand(); # [0,1)之间的小数
	select pow(2,3); # 8 求幂
字符函数
	select length('ab cd'); # 5 求长度
	select lower('This'); # this 转化为小写
	select upper('This'); # THIS 转化为大写
	select substr('ab cd', 3, 2); # c sql中的下标从3开始 从下标为3的位置去两个元素
	select replace('abc d', ' ', 'o'); # abcod 替换
	select trim(' abcd '); # abcd 删除开头与结尾的空格
	select lpad('ab',5,'*'); # ***ab 总长度为5 若5变为1 结果为a 左扩充
	select rpad('ab',5,'*'); # ab*** 同理其结果也为a 右扩充                                                                                                                                            
日期函数
	select now(); # 现在国际日期+时间
	select sysdate(); # 系统日期+时间
	select current_date(); # 国际日期
	select current_time(); # 国际时间
	select year(now()); # 获取年份
	select month(now()); # 获取月份
	select day(now()); # 获取日期
	select hour(now()); # 获取小时
	select minute(now()); # 获取分钟
	select second(now()); # 获取秒
聚合函数
	select max(score) from student_scores;# 求最大值
	select min(score) from student_scores;# 求最小值
	select avg(score) from student_scores;# 求平均值
	select decimal count(score) from student_scores;# 求数量
	select sum(num) from student_scores;# 求和
分组函数
	select count(score) from student_scores group by class_name having score > 60; # 求每个班级成绩>60的数量
加密函数
	select md5('root');
	select sha('root');
	select password('root');

猜你喜欢

转载自blog.csdn.net/qq_31307291/article/details/87879019