MySQL常用函数
一、函数分类:
1.字符串函数;
2.数值函数;
3.日期和时间函数;
4.流程函数;
5.其它函数;
二、常用函数:
1.字符串函数:
(1)concat(s1,s2,,,sn);//连接所有传入的参数;任何字符串和null连接都为null;
(2)insert(str,x,y,instr);//将字符串str从x开始的y位用instr替代;x从1开始,且包含本身;
(3)lower()和upper();//将传入字符串转为小写和大写;
(4)left(str,x)和right(str,y);//分别将字符串str从左边和右边截取x长度显示出来;
(5)lpad(str,n,pad)和rpad(str,n,pad);//在str字符串的左边和右边增加pad字符串,直到str字符串总数到达n个;
(6)ltrim(str)和rtrim(str);//去掉字符串当中最左侧和最右侧的空格;
(7)trim(str);//去掉两头空格;
(8)repeat(str,x);//重复x遍str;
(9)replace(str,a,b);//把字符串str中的所有a用b替换;
(10)substring(str,x,y);//把字符串str从第x个开始截取y个显示出来;x从 1开始,且包含本身;
2.数值函数:
(1)abs(x);//返回x的绝对值;
(2)ceil(x);//向上取整;1.2取2;
(3)floor(x);//向下取整;1.8取1;
(4)mod(x,y);//取x/y的余数;
(5)rand();//返回一个0~1的值,[0,1);
3.日期函数:
(1)curdate();//返回当前时间;2019-01-01;
(2)curtime();//返回当前时间;00:00:00;
(3)now();//返回日期加时间;
//sysdate();功能相同;
(4)unix_timestam();//时间戳,1970年开始的毫秒数;
(5)from_unixtime(unixtime);将一个时间戳转换为日期;
(6)week(date);//返回所给日期是一年中的第几周;
(7)year(date);//返回所给日期是哪一年;
(8)hour(time);//返回所给时间的小时;
(9)minute(time);//返回所给时间的分钟;
(10)date_format(date,fmt);//按fmt中字符串格式转化date值;%d %y查表;
(11)date_add(date,interval exper type);//计算日期间隔;例:date_add(now(),interval -3 year);
(12)datediff(date1,date2);//计算两个日期相差天数;例:datediff('2018-1-1','2019-1-1'),返回-365;
4.流程函数:
(1)if(value,t,f);//如果value为真返回t,否则返回f;
(2)ifnull(value1,value2);//如果value1不为null返回value1,否则返回value2;
(3)case when then end;//例:select case when 2>0 then ‘对’ else ‘错’ end;
5.其他函数:
(1)database();//返回当前数据库名称;
(2) version();//返回当前数据库版本号;
(3)user();//返回当前用户;
(4)password(str);//对str加密;新版中已经移除;
(5)MD5(str);//返回str的MD5值;