MYSQL 常用函数

GROUP_CONCAT() 该函数返回带有来自一个组的连接的非NULL值的字符串结果

以后项目课再演示!!!

ifnull(exp1,exp2) 如果为空

select ifnull(a,'无') 如果a字段有空数据则显示为'无'

if(exp1,exp2,exp3)

select if(male=0,'男','女') 如果male字段等于0显示'男'否则显示'女'

一、数学函数:

ABS 绝对值函数

select abs(-5) ;

ceil 天花板整数,也就是大于x的整数

select ceil(-13.5);

FLOOR(X)小于x的最大整数

select FLOOR(3.5);

返回集合中最大的值,least返回最小的,注意跟max,min不一样,max里面跟的是col,返回这个列的最大值最小值

select GREATEST(1,2,3,4);

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);

truncate(x,y) 返回数字x截断为y位小数的结果,就是不考虑四舍五入,直接砍掉

select TRUNCATE(pi(),4),ROUND(pi(),4),pi();

sign(x) 返回x符号

select sign(-5);

二、聚合函数(常用语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

三、字符串函数

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);

substring(str,len)从左到右从str中选择长度为len的字符串截取至尾

select substring('hello world',4);

length(str)返回str字符串长度

select length('hello world');

trim(),ltrim()rtrim()分别去掉两头的空格,左边的空格,右边的空格

select trim(' hello ');

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')

四、日期和时间函数

datediff(date1,date2)返回两个日期相隔的天数

SELECT DATEDIFF('2008-12-30','2008-12-29');

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); --返回当前日期后5天 参数可为负
select date_add(curtime(),interval 5 hour); --返回当前时间后5个小时 参数可为负

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());

year()返回年

select year(curdate());

minute()返回分钟

select week(now());

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)计算两个时间差,还有datediff()

select TIMESTAMPDIFF(year,19920202,CURDATE())

DATE_FORMAT(date,format)按照格式转化日期,具体的format格式可以查查

select date_format(CURDATE(),'%m-%d-%Y');

%a 缩写星期名
%b 缩写月名
%c 月,数值
%D 带有英文前缀的月中的天
%d 月的天,数值(00-31)
%e 月的天,数值(0-31)
%f 微秒
%H 小时 (00-23)
%h 小时 (01-12)
%I 小时 (01-12)
%i 分钟,数值(00-59)
%j 年的天 (001-366)
%k 小时 (0-23)
%l 小时 (1-12)
%M 月名
%m 月,数值(00-12)
%p AM 或 PM
%r 时间,12-小时(hh:mm:ss AM 或 PM)
%S 秒(00-59)
%s 秒(00-59)
%T 时间, 24-小时 (hh:mm:ss)
%U 周 (00-53) 星期日是一周的第一天
%u 周 (00-53) 星期一是一周的第一天
%V 周 (01-53) 星期日是一周的第一天,与 %X 使用
%v 周 (01-53) 星期一是一周的第一天,与 %x 使用
%W 星期名
%w 周的天 (0=星期日, 6=星期六)
%X 年,其中的星期日是周的第一天,4 位,与 %V 使用
%x 年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y 年,4 位
%y 年,2 位

你可能感兴趣的:(MYSQL 常用函数)