MySQL-----常用函数整理

/*日期函数*/


select year(now()); #返回年份


select month(localtime);#返回月份


select sysdate(); #返回当前时间


select(localtime);#返回当前时间,注:now()、localtime()、localtime、sysdate()


select now(); #返回当前时间


select monthname(now()); #返回月份的英文名


/*数值函数*/


select truncate(3.9999,2); #truncate(x,y),x是小数,y是取x的几位数


select abs(-3.99); #取绝对值


select rand(); #获取0-1之间的随机数


select floor(9 + (rand() * 8));   #若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1))

/*流程函数*/


1、if(expr1,expr2,expr3)  expr1如果为true,则返回expr2,如果为false,则返回expr3

示例:select if(age=20,100,age)from user;

2、IFNULL(expr1, expr2)   如果expr1不为空,则返回expr1,为空则返回expr2的值

示例:select ifnull(username,"空") from user;

3、case when (value) then (result) else (default) END


示例:select case when age>27 then "最大" else age end from student;

原文地址:https://www.cnblogs.com/syw20170419/p/10213828.html