【MySQL】P5 SQL函数
创始人
2024-05-29 20:44:39
0

SQL函数

  • 字符串函数
  • 数值函数
  • 日期函数
  • 流程函数

字符串函数


函数功能
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;

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...