Knowledge note
03-函数
#字符串函数

-- 链接字符串
select concat('huangjisheng','dashuaige');
-- 全部转换为小写
select lower('HUANGjiSHENG');
-- 全部转换为大写
select upper('world');
-- 左填充
select LPAD('world',10,'-');
-- 右填充
select rpad('world',10,'-');
-- 去掉字符串头部和尾部的空格
select trim(' nihao, shijei ');
-- 返回从字符串str从开始位置起的len个长度的字符串
select substring('abcdefghijklmnopqrstuvwxyyz',5,9);
-- 更新user表字段name,name为5位,不足的地方左边补0
update test.user set name=lpad(name,5,'0');
#数值函数

-- 向上取整
select ceil(1.3);
-- 向下取整
select floor(1.3);
-- 返回x/y的模
select MOD(5,2);
-- 返回0~1内的随机数
select RAND();
#日期函数

-- 返回当前日期
select curdate();
-- 返回当前时间
select curtime();
-- 返回当前日期和时间
select NOW();
-- 获取指定data的年份
select year(curdate());
-- 获取指定data的月份
select MONTH(curdate());
-- 获取指定data的日期
select day(curdate());
-- 返回上一个日期/时间值加上一个时间间隔expr后的时间值
select date_add(now(),interval 10 year );
-- 返回起始时间和结束时间之间的天数
select datediff('2025-08-17','2028-09-10');
#流程函数

-- if
select if(true,'真','假');
-- ifnull___ok
select ifnull('ok','default');
-- ___空
select ifnull('','default');
-- ___default
select ifnull(null,'default');
-- case when then else end
select
name,
(case user.gender when '男' then '力气大' else '力气小'end )as '力气大小'
from user;