函数 | 功能 |
---|---|
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个长度的字符串 |
select concat('hello ', 'MySQL');
# 拼接结果为:'hello MySQL';
select lower('Hello');
# 输出结果为:'hello';
select upper('hello');
# 输出结果为:'HELLO';
select lpad('01',5,'-');
# 输出结果为:'---01';
select rpad('01',5,'-');
# 输出结果为:'01---';
select trim(' hello mysql ');
# 输出结果为:'hello mysql';
select substring('Hello MySQL', 1, 5);
# 输出结果为:'Hello';
# 代码含义为:将'Hello MySQL'从第一个字符开始截取,截取5个字符;
e.g.e.g.e.g.
在员工表 emp 中,将所有工号 workno 补足为5位,用0填充;
update emp set workno = lpad(workno,5,0);
函数 | 功能 |
---|---|
ceil(x) | 向上取整 |
floor(x) | 向下取整 |
mod(x,y) | 返回x/y的模 |
rand() | 返回0-1内的随机数 |
round(x,y) | 求参数x的四舍五入的值,保留y位小数 |
select ceil(1.1);
# 向上取整,结果为2;
select floor(1.9);
# 向下取整,结果为1;
select mod(5,4);
# 取模,结果为1;
select rand();
# 取0-1的随机数;
select round(2.345,2);
# 四舍五入,保留(2.345,2):对2.345做四舍五入,保留2位小数;结果为2.35;
e.g.e.g.e.g.
通过数据库函数,生成一个六位数的随机验证码;
select rpad(ceil(rand()*1000000),6,'0');
# 通过rand()生成0-1之间的随机数字,*1000000后进行四舍五入,保留0位小数,然后再将五位数向右或者向左补零
函数 | 功能 |
---|---|
curdate() | 返回当前日期 |
curtime() | 返回当前时间 |
now() | 返回当前日期和时间 |
year(date) | 返回指定date的年份 |
month(date) | 返回指定date的月份 |
day(date) | 返回指定date的日期 |
date_add(date, interval expr type) | 返回一个日期值加上一个时间间隔expr后的时间值 |
datediff(date1,date2) | 返回 起始日期date1 与 结束时间date2 之间的天数 |
select curdate();
# 返回当前年月日:2023-03-06
select curtime();
# 返回当前时分秒:20:18:50
select now();
# 返回当前年月日时分秒:2023-03-06 20:19:12
select year(now());
# 返回当前年份:2023
select month(now());
# 返回当前月份:3
select day(now());
# 返回当前日期:6
select date_add(now(), interval 70 day );
# 返回当前年月日时分秒 + 70 天之后的年月日时分秒:2023-05-15 20:22:42
select date_add(now(), interval 70 month );
# 返回当前年月日时分秒 + 70 月之后的年月日时分秒:2029-01-06 20:21:44
select date_add(now(), interval 70 year );
# 返回当前年月日时分秒 + 70 月之后的年月日时分秒:2093-03-06 20:23:15
select datediff('2021-12-1', '2021-11-1');
# 返回两个日子差多少天:30;
select datediff('2021-11-1', '2021-12-1');
# 返回两个日子差多少天:-30;
e.g.e.g.e.g.
查询所有员工的入职天数,并根据入职天数倒序排序
select name, datediff(now(),entrydate) as 'entrydates' from emp order by entrydates desc);
函数 | 功能 |
---|---|
if(value,t,f) | 如果value为true,返回t,否则返回f |
ifnull(value1, value2) | 如果value1不为空,返回value1,否则返回value2 |
case when val1 then res1 … else default end | 如果val为true,返回res1,…,否则返回default |
case expr when val1 then res1 … else default end | 如果expr的值等于val1,返回res1,… 否则返回default默认值 |
select if(true, 'ok', 'error');
# 返回结果为ok
select if(false, 'ok', 'error');
# 返回结果为error
select ifnull('','default');
# 返回结果为,因为空串不是null值;
select ifnull(null,'default');
# 返回结果为default;
e.g.e.g.e.g.
表记录学生的数学,英语以及语文成绩;如果成绩大于等于85,优秀;如果成绩大于等于60,及格;其余为不及格。
select id,name,
(case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end) as '数学',
(case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end) as '英语',
(case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end) as '语文'
from score;
e.g.e.g.e.g.
查询员工姓名以及工作地址(北京/上海 标记为 一线城市,其余城市 标记为 二线城市)
select name, (case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as '工作地址'
from emp;
上一篇:sql开窗函数