MySQL函数
1、字符串函数
| 函数 | 说明 | 
|---|---|
| concat(s1, s2, ..., sn) | 字符串拼接,将 s1, s2, ..., sn 拼接成一个字符串 | 
| lower(str) | 将字符串 str 全部转为小写 | 
| upper(str) | 将字符串 str 全部转为大写 | 
| lpad(str, n, pad) | 左填充,用字符串 pad 对 str 的左边进行填充,达到 n 个字符串长度 | 
| rpad(str, n, pad) | 右填充,用字符串 pad 对 str 的右边进行填充,达到 n 个字符串长度 | 
| trim(str) | 去掉字符串头部和尾部的空格 | 
| substring(str, start, len) | 返回从字符串 str 从 start 位置起的 len 个长度的字符串, 索引从 1 开始 | 
            
            
              sql
              
              
            
          
          -- concat
select concat('hello', 'MySQL');
-- lower
select lower('Hello');
-- upper
select upper('Hello');
-- lpad
select lpad('hhh', 5, '-');
-- rpad
select rpad('hhh', 5, '-');
-- trim
select trim('  Hello MySQL!  ');
-- substring
select substring('Hello MySQL', 1, 5);2、数值函数
| 函数 | 说明 | 
|---|---|
| ceil(x) | 向上取整 | 
| floor(x) | 向下取整 | 
| mod(x, y) | 返回 x/y 的模 | 
| rand() | 返回 0~1 内的随机数 | 
| round(x, y) | 求参数 x 的四舍五入的值,保留 y 为小数 | 
            
            
              sql
              
              
            
          
          -- ceil
select ceil(1.1);
-- floor
select floor(1.1);
-- mod
select mod(7, 4);
-- rand
select rand();
-- round
select round(1.254, 1);3、日期函数
| 函数 | 说明 | 
|---|---|
| curdate() | 返回当前日期(年-月-日) | 
| curtime() | 返回当前时间(时-分-秒) | 
| now() | 返回当前日期和时间 | 
| YEAR(date) | 获取指定 date 的年份 | 
| MONTH(date) | 获取指定 date 的月份 | 
| DAY(date) | 获取指定 date 的日期 | 
| DATE_ADD(date, INTERVAL expr type) | 返回一个日期/时间值加上一个时间间隔 expr 后的时间值 | 
| datediff(date1, date2) | 返回起始时间 date1 和结束时间 date2 之间的天数 | 
            
            
              sql
              
              
            
          
          -- curdate
select curdate();
-- curtime
select curtime();
-- now
select now();
-- YEAR
select YEAR(now());
-- MONTH
select MONTH(now());
-- DAY
select DAY(now());
-- DATE_ADD
select DATE_ADD(now(), INTERVAL 10 DAY);
-- datediff
select datediff('2021-10-01', '2021-06-01');4、流程函数
| 函数 | 说明 | 
|---|---|
| if(value, t, f) | 如果 value 为 true, 则返回 t, 否则返回 f | 
| ifnull(value1, value2) | 如果 value1 不为空, 返回 value1, 否则返回value2 | 
| case when [val1] then [res1] when [val2] then [res2] ... else [default] end | 如果 val1 为 true, 返回 res1,...,否则返回 default 默认值 | 
| case [expr] when [val1] then [res1] when [val2] then [res2] ... else [default] end | 如果 expr 的值等于 val1,返回 res1,...,否则返回 default 默认值 | 
            
            
              sql
              
              
            
          
          -- if(value, t, f)
select if(true, 'True', 'False');
select if(false, 'True', 'False');
-- ifnull(value1, value2)
select ifnull('OK', 'Default');
select ifnull('', 'Default');
select ifnull(null, 'Default');
-- case when [val1] then [res1] when [val2] then [res2] ... else [default] end
select 
	id, 
	name,
	(case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end) as math,
	(case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end) as english,
	(case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end) as chinese
from 
	score;
-- case [expr] when [val1] then [res1] when [val2] then [res2] ... else [default] end
select
	id,
	name,
	(case address when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as address
from 
	tb_emp;