MySQL常用函数

一、函数分类:

  1.字符串函数;

  2.数值函数;

  3.日期和时间函数;

  4.流程函数;

  5.其它函数;

二、常用函数:

1.字符串函数:

1concat(s1s2,,,sn)//连接所有传入的参数;任何字符串和null连接都为null;

2insert(strxyinstr)//将字符串str从x开始的y位用instr替代;x从1开始,且包含本身;

3lower()upper()//将传入字符串转为小写和大写;

4left(strx)right(stry)//分别将字符串str从左边和右边截取x长度显示出来;

5lpad(strnpad)rpad(strnpad)//在str字符串的左边和右边增加pad字符串,直到str字符串总数到达n个;

6ltrim(str)rtrim(str)//去掉字符串当中最左侧和最右侧的空格;

7trim(str)//去掉两头空格;

8repeat(strx)//重复x遍str;

9replace(strab)//把字符串str中的所有a用b替换;

10substring(strxy)//把字符串str从第x个开始截取y个显示出来;x从 1开始,且包含本身;

2.数值函数:

1abs(x)//返回x的绝对值;

2ceil(x)//向上取整;1.2取2;

3floor(x)//向下取整;1.8取1;

4mod(xy);//取x/y的余数;

5rand()//返回一个0~1的值,[0,1);

3.日期函数:

 1curdate()//返回当前时间;2019-01-01;

 2curtime();//返回当前时间;00:00:00;

 3now()//返回日期加时间;

  //sysdate();功能相同;

4unix_timestam()//时间戳,1970年开始的毫秒数;

5from_unixtime(unixtime)将一个时间戳转换为日期;

6week(date)//返回所给日期是一年中的第几周;

7year(date)//返回所给日期是哪一年;

8hour(time)//返回所给时间的小时;

9minute(time)//返回所给时间的分钟;

10date_format(datefmt)//按fmt中字符串格式转化date值;%d %y查表;

11date_add(dateinterval exper type)//计算日期间隔;例:date_add(now(),interval -3 year);

12datediff(date1date2)//计算两个日期相差天数;例:datediff('2018-1-1','2019-1-1'),返回-365;

4.流程函数:

1if(valuetf)//如果value为真返回t,否则返回f;

2ifnull(value1value2)//如果value1不为null返回value1,否则返回value2;

3case when then end//例:select case when 2>0 then ‘对’ else ‘错’ end;

5.其他函数:

(1)database();//返回当前数据库名称;

(2) version();//返回当前数据库版本号;

(3)user();//返回当前用户;

(4)password(str);//对str加密;新版中已经移除;

5MD5(str)//返回str的MD5值;

 

posted @ 2019-08-01 22:29  开拖拉机的拉风少年  阅读(130)  评论(0编辑  收藏  举报