语法:select 函数
常见的字符串函数
总结:
字符串函数:
concat(字符串拼接):concat(s1,s2,s3.....)
lower(字符串全部转小写):lower(str)
upper(字符串全部转大写):upper(str)
lpad(左填充):lpad(str,字符串填充后的长度(包括原始字符串, 填充字符)
rpad(右填充):lpad(str,字符串填充后的长度(包括原始字符串, 填充字符)
trim(去除字符串头部和尾部空格):trim(str)
substring(截取字符串):substring(str,起始位置,结束位置)
数值函数:
ceil(向上取整):ceil(x)
floor(向下取整):floor(x)
mod(返回x/y的余数):mod(x,y)
rand(返回0-1的随机数):rand()
round(四舍五入):round(x,小数后保留位数)
日期函数:
curdate(获取当前日期):curdate()
curtime(获取当前时间):curtime()
now(获取当前日期和时间):now()
year(获取年份):year(date)
month(获取月份):month(date)
day(获取日期):day(date)
date_add(获取一个日期/时间值间隔一定时间后的时间值):date_add(date,interval 天数 时间单位)
datefiff(获取两个时间之间的天数):datediff(date1,date2)
流程函数:
if(value,t,f):如果value为true,则返回t,否则返回f
ifnull(value1,value2):如果value1不为空,返回value1,否则返回value2
case when[val1] then [res1]...else[default] end:如果val1为true,返回res1,...否则返回dafault默认值
字符串函数
concat(字符串拼接)
select concat(s1,s2,s3.....);
select concat("hello"," mysql");
lower(字符串全部转小写)
select lower(str);
select lower("HELLO");
upper(字符串全部转大写)
select upper(str);
select upper("hello");
lpad(左填充)
select lpad(str,字符串填充后的长度(包括原始字符串, 填充字符);
select lpad("01",5,"-");
rpad(右填充)
select lpad(str,字符串填充后的长度(包括原始字符串, 填充字符);
select rpad("01",5,"-");
trim(去除字符串头部和尾部空格)
select trim(str);
select trim(" hello mysql ");
substring(截取字符串)
select substring(str,起始位置,结束位置);
注:索引从1开始,截取的字符串包含结束位置
select substring("hello mysql",1,5);
eg:
将员工的工号统一为5位数,不足5位数的在工号前面补充0,如:00001
update emp set workno = lpad(workno,5,"0");
数值函数
语法:select 函数
常见数值函数
ceil(向上取整)
select ceil(x);
select ceil(1.5)
floor(向下取整)
select floor(x);
select floor(1.9);
mod(返回x/y的余数)
select mod(x,y);
select mod(5,4);
rand(返回0-1的随机数)
select rand();
round(四舍五入)
select round(x,小数后保留位数);
select round(2.345,2);
eg:
生成一个六位数的随机数
select lpad(round(rand(),6)*1000000,6,"0");
日期函数
语法:select 函数
常见日期函数
curdate(获取当前日期)
select curdate();
curtime(获取当前时间)
select curtime();
now(获取当前日期和时间)
select now();
year(获取年份)
select year(date);
select year(now());
month(获取月份)
select month(date);
select month(now());
day(获取日期)
select day(date);
select day(now());
date_add(获取一个日期/时间值间隔一定时间后的时间值)
select date_add(date,interval 天数 时间单位);
时间单位:year(年),month(月),day(天)...
select date_add(now(),interval 70 day);
datefiff(获取两个时间之间的天数)
select datediff(date1,date2);
date1-date2
select datediff(now(),"2020-01-01");
eg:
查询所有员工入职天数,根据入职天数倒叙排序
select name,datediff(now(),entrydate) days from emp order by days desc;
流程函数
常见函数
if(value,t,f)
如果value为true,则返回t,否则返回f
select if(1>2,1,2);
ifnull(value1,value2)
如果value1不为空,返回value1,否则返回value2
select ifnull(null,"2");
case when[val1] then [res1]...else[default] end
如果val1为true,返回res1,...否则返回dafault默认值
eg:
查询员工姓名和工作地址,将北京上海改成一线城市,其他城市改成二线城市
select 字段名, (case when 条件 then 返回值1 else 返回值2 end) as 别名 from 表名;
select name, (case when workaddress = "北京" or workaddress = "上海" then "一线城市" else "二线城市" end) as "工作地址" from emp;
eg:
展示班级每个学员成绩,>=85,优秀;>=60,及格;否则不及格
建表信息:
CREATE TABLE score(
id int comment 'ID',
name VARCHAR(20) COMMENT '姓名',
math int COMMENT '数学',
english int COMMENT '英语',
chinese int COMMENT '语文'
)COMMENT '学员成绩表';
INSERT into score(id,name,math,english,chinese) VALUES
(1,'Tom',67,88,95),(2,'Rose',23,66,90),(3,'Jack',56,98,76);
select 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;