一、数学函数:
#ABS 绝对值函数select abs(-5) ;#BIN 返回二进制,OCT()八进制,hex十六进制select bin(2);#ceiling 天花板整数,也就是大于x的整数select CEILING(-13.5);#EXP(X)自然对数e为底的x次方select EXP(2);#FLOOR(X)小于x的最大整数select FLOOR(3.5);#返回集合中最大的值,least返回最小的,注意跟max,min不一样,max里面跟的是col,返回这个列的最大值最小值select GREATEST(1,2,3,4);#ln(x)返回对数select LN(10);#log(x,y),以y为底x的对数select log(4,2);#mod(x,y)返回x/y的余数select mod(5,2);#pi() 返回piselect pi();#RAND()返回0,1之间随机数select rand();#round(x,y)返回x四舍五入有y位小数的值select round(pi(),3);#sign(x) 返回x符号select sign(-5);#sqrt(x)返回x的平方根select sqrt(9);#truncate(x,y) 返回数字x截断为y位小数的结果,就是不考虑四舍五入,直接砍掉select TRUNCATE(pi(),4),ROUND(pi(),4),pi();
二、聚合函数(常用语group by从句select语句中)
#avg(col)返回指定列平均值select AVG( quantity) from orderitems;#count(col)返回指定列中非null数量select count(quantity) from orderitems;#min(col)max(col)返回指定列最大最小值select max(quantity),min(quantity) from orderitems;#sum(col)返回制定列求和值select sum(quantity) from orderitems;#group_concat(col)返回这个列连接组合的结果,中间有逗号隔开select group_concat(prod_id) from orderitems
三、字符串函数
#ASCII(str)返回字符串的ascii码值select ASCII('12');#bit_length(str)返回字符串的比特长度select bit_length('123');#concat()连接字符串select concat('1','我','2');#concat_ws(sep,s1,s2,s3)连接字符串用sep隔开select concat_ws('|','1','我','2');#INSERT(str,pos,len,newstr),将字符串str从pos位置开始的len长度替换为newstrselect 'hello world',INSERT('hello world',2,3,'杰哥哥');#FIND_IN_SET(str,strlist)分析逗号分隔的list,如果发现str返回list中的位置select find_in_set('abc','aabc,sdf,det,abc') #返回4#LCASE(str),LOWER(str)都是返回小写的结果,UPPER(),UCASE() 返回的都是大写结果select LCASE('UUSU');#`LEFT`(str,len)从左到右从str中选择长度为len的字符串,`RIGHT`(str,len)相反select left('hello world',4);#length(str)返回str字符串长度select length('hello world');#trim(),ltrim()rtrim()分别去掉两头的空格,左边的空格,右边的空格select trim(' hello ');#POSITION(substr IN str),返回substr首次在str中长线的位置select POSITION('llo' IN 'hello world');#QUOTE(str)用反斜杠转义str中的单引号select QUOTE("hello ' world")#`REPLACE`(str,from_str,to_str)在str中,把from str 转成to_strselect replace('hello world','hello','hi');#repeat(str,count)重复str count次select repeat('hello world',3);#reverse(str)颠倒strselect reverse('hello world');#STRCMP(expr1,expr2)比较两个字符串,一模一样返回0,不一样返回1select STRCMP('hello','world')
四、日期和时间函数
#curdate()或者current_date() NOW()是返回日期+时间select current_date();#curtime()当前时间,或者current_time()select curtime();#DATE_ADD(date,INTERVAL expr unit)返回date加上int日期后的日期,date_sub()也是一样的select date_add(curdate(),interval 5 day);select date_add(curtime(),interval 5 hour);#DATE_FORMAT(date,format)按照格式转化日期,具体的format格式可以查查select date_format(CURDATE(),'%m-%d-%Y');#dayofweek()DAYOFMONTH(date)DAYOFYEAR(date)返回日期中是一周中第几天,一个月中第几天,一年中第几天select DAYOFMONTH(CURDATE());#dayname返回日期星期名select dayname(CURDATE());#hour(time),minute(time),month(date)select curtime(),hour(CURTIME()),minute(curtime());select curdate(),month(curdate()),year(curdate()),day(curdate());#MONTHNAME(date) 返回月份名称select monthname(curdate());#quarter(date)返回日期的第几季度select quarter(curdate());#week(date)select week(curdate());#TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)计算两个时间差,还有datediff()select TIMESTAMPDIFF(year,19920202,CURDATE())
参考博客:https://blog.csdn.net/sugang_ximi/article/details/6664748