MySQL—函数大全

一、数学函数:

#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() 返回pi
select 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长度替换为newstr
select '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_str
select replace('hello world','hello','hi');
#repeat(str,count)重复str count次
select repeat('hello world',3);
#reverse(str)颠倒str
select reverse('hello world');
#STRCMP(expr1,expr2)比较两个字符串,一模一样返回0,不一样返回1
select 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

posted @ 2018-03-26 10:45  慢慢来会比较快  阅读(4373)  评论(0编辑  收藏  举报