![c07e5303af8abe1194e700d7cfbb8417.png](https://img-blog.csdnimg.cn/img_convert/c07e5303af8abe1194e700d7cfbb8417.png)
上期讲解了MySQL的一些基础知识(面试必备sql知识点——MySQL基础)
本期继续深化知识点,基于基础,做进一步延伸。学习就是个不断反刍的过程,将碎片化的知识,搭建成完整的框架体系,才能够真正掌握。如果对你有帮助,建议点赞收藏!
内容大纲:
![3dd806799882cc323934672725c9ddf8.png](https://img-blog.csdnimg.cn/img_convert/3dd806799882cc323934672725c9ddf8.png)
一、条件
CASE表达式
case 函数的格式为:case when 判断表达式 then 表达式 else 表达式 end
else 可省略,end不可省略。
- 连续数值转离散分类
![e17cb01b09f97c29b9faea0f2571123e.png](https://img-blog.csdnimg.cn/img_convert/e17cb01b09f97c29b9faea0f2571123e.png)
--借款金额分组
- 行列转换
![fe8a70a8a0708c1f24a37e5e959da157.png](https://img-blog.csdnimg.cn/img_convert/fe8a70a8a0708c1f24a37e5e959da157.png)
SELECT
![b813b1dcc5ece4488054a2a83bb5a7a5.png](https://img-blog.csdnimg.cn/img_convert/b813b1dcc5ece4488054a2a83bb5a7a5.png)
if函数
if函数格式为:if(条件,条件为真时的值,条件为假时的值)
如上述行列转换也可以这样写,得到的结果一致。
SELECT
![e88ea807ccab64d9f895c5a5ba15bf2c.png](https://img-blog.csdnimg.cn/img_convert/e88ea807ccab64d9f895c5a5ba15bf2c.png)
但借款金额的分组就不如case when 书写方便了,但在计数算比列方面,if的写法可以减少查询的嵌套。
二、字符串函数
常用
- concat 字符串拼接 —— concat(字符串,字符串···)
![e65bc7b225f5e632b946f973b379b81d.png](https://img-blog.csdnimg.cn/img_convert/e65bc7b225f5e632b946f973b379b81d.png)
SELECT
![a62ebcbe02be8448d903cbf49e308676.png](https://img-blog.csdnimg.cn/img_convert/a62ebcbe02be8448d903cbf49e308676.png)
- replace 字符串替换 —— replace(字符串,被替换的字符串,替换字符串)
-- 改变上表中str1列的值
![aac88c3249a64db42edf20f6673d74bb.png](https://img-blog.csdnimg.cn/img_convert/aac88c3249a64db42edf20f6673d74bb.png)
- substring字符串截取——substring(对象字符串 from 起始位置 for 截取字符数)
![ccff1fed67f17d2cc45be68cee9d7060.png](https://img-blog.csdnimg.cn/img_convert/ccff1fed67f17d2cc45be68cee9d7060.png)
UPDATE
![a98be54d7ae6a50ccd86387bd0f45b3c.png](https://img-blog.csdnimg.cn/img_convert/a98be54d7ae6a50ccd86387bd0f45b3c.png)
不常用
- length长度——length()
select
![f36d1da73a002ecc28f506e91399e27f.png](https://img-blog.csdnimg.cn/img_convert/f36d1da73a002ecc28f506e91399e27f.png)
- lower小写——lower()
![87aefa644550a200f53bcdb785ff3e10.png](https://img-blog.csdnimg.cn/img_convert/87aefa644550a200f53bcdb785ff3e10.png)
select
![84cae8e46317fae1125dabf96259ab3f.png](https://img-blog.csdnimg.cn/img_convert/84cae8e46317fae1125dabf96259ab3f.png)
- upper大写——upper()
select
![b9ebd9f9d78e6dd744bf858327c4e866.png](https://img-blog.csdnimg.cn/img_convert/b9ebd9f9d78e6dd744bf858327c4e866.png)
类型转换
cast——cast(值 as 类型)
select
![2438a8d62036049cac97ce4d0dbe44a0.png](https://img-blog.csdnimg.cn/img_convert/2438a8d62036049cac97ce4d0dbe44a0.png)
三、日期函数
日期转换
- str_to_date(format,date):将指定字符串格式转换成日期
SELECT
![4102e3bbebbe6c57d70281eb7d551a57.png](https://img-blog.csdnimg.cn/img_convert/4102e3bbebbe6c57d70281eb7d551a57.png)
- date_format(date,format):将日期转换成指定字符串格式
SELECT
![528d38ce592420d9451e8ffdc43b2b51.png](https://img-blog.csdnimg.cn/img_convert/528d38ce592420d9451e8ffdc43b2b51.png)
- from_unixtime(unix_timestamp,format):将时间戳转化为日期
SELECT
![2b3f604becf03a0a769994ac58d7982e.png](https://img-blog.csdnimg.cn/img_convert/2b3f604becf03a0a769994ac58d7982e.png)
日期提取
- now( ) ——返回当前系统日期+时间
SELECT
![fb8211fb530e9f1d56187207c8a8273c.png](https://img-blog.csdnimg.cn/img_convert/fb8211fb530e9f1d56187207c8a8273c.png)
- curdate( )——返回当前系统日期,不包含时间
SELECT
![a09c4b909b11fcc9b3678cf726c86f09.png](https://img-blog.csdnimg.cn/img_convert/a09c4b909b11fcc9b3678cf726c86f09.png)
- curtime( ) ——返回当前时间,不包含日期
SELECT
![dd49c612dee8da67b0626c5f4db35ea3.png](https://img-blog.csdnimg.cn/img_convert/dd49c612dee8da67b0626c5f4db35ea3.png)
- year ()month()day()——获取日期年月日(%Y-%m-%d)
SELECT
![5c9e5d3a5d8b6312eb49fb5514ba12a1.png](https://img-blog.csdnimg.cn/img_convert/5c9e5d3a5d8b6312eb49fb5514ba12a1.png)
- hour()minute ( ) second( ) ——获取日期时分秒(%h:%i:%s)
SELECT
![762d6ced267885d7e878676898dcf40e.png](https://img-blog.csdnimg.cn/img_convert/762d6ced267885d7e878676898dcf40e.png)
日期运算
- datediff——datediff(date1,date2) 返回起始时间 date1 和结束时间 date2 之间的天数。
SELECT
![b9d9ab870240d5539cf39c462f2b8bdc.png](https://img-blog.csdnimg.cn/img_convert/b9d9ab870240d5539cf39c462f2b8bdc.png)
- date_sub——date_sub(date,INTERVAL expr type) 表示从日期减去指定的时间间隔后的日期,date参数是日期表达式。expr 参数是时间间隔,type 参数有很多,常用的是day。
SELECT
![261ce03086de64f940b468fbd51f18b9.png](https://img-blog.csdnimg.cn/img_convert/261ce03086de64f940b468fbd51f18b9.png)
- dateadd——date_add(date,INTERVAL expr type) 表示向日期添加指定的时间间隔后的日期
SELECT
![8ea2e4c0f0f03f5d09661c05de6a8e9d.png](https://img-blog.csdnimg.cn/img_convert/8ea2e4c0f0f03f5d09661c05de6a8e9d.png)
四、算术函数
- + - * / —— 加减乘除:比较简单常用
- round —— round(数值,保留小数的位数):对数据四舍五入处理
select
![aba608356acb125d79832b51e724b03c.png](https://img-blog.csdnimg.cn/img_convert/aba608356acb125d79832b51e724b03c.png)
- abs——abs(数值):求绝对值
select
![5f4dcd27bc9acc56d813e9a9b6e42b10.png](https://img-blog.csdnimg.cn/img_convert/5f4dcd27bc9acc56d813e9a9b6e42b10.png)
- mod——mod(被除数,除数):求余数
select
![e927d5d9210f0eeb86ceff93078f995a.png](https://img-blog.csdnimg.cn/img_convert/e927d5d9210f0eeb86ceff93078f995a.png)
五、窗口函数
窗口函数语法: 窗口函数 over(partition by 列名 order by 列名)
partition by 后接的是分组列名 (可根据需求省略)
order by 后接的是排序的列名
简单来说,窗口函数的作用就是将数据表先分组后排序,然后根据选用的窗口函数来实现具体的功能。
mysql中窗口函数可以分为两类
【1】专用窗口函数(排序):
![bbd6080bc11d44787039929090d70478.png](https://img-blog.csdnimg.cn/img_convert/bbd6080bc11d44787039929090d70478.png)
- rank 函数:计算排序时,存在相同位次记录,则会跳过之后位次:1,1,1,4····
-- rank()over(partition by 列名 order by 列名)
![54a48e4961ab1ab0532518134af28ec7.png](https://img-blog.csdnimg.cn/img_convert/54a48e4961ab1ab0532518134af28ec7.png)
- dense_rank函数:计算排序时,存在相同位次,也不会跳过之后的位次:1,1,1,2····
-- dense_rank()over(partition by 列名 order by 列名)
![ed3b704cfbf4d43ff70988184a89ef0a.png](https://img-blog.csdnimg.cn/img_convert/ed3b704cfbf4d43ff70988184a89ef0a.png)
- row_number函数:计算排序时,哪怕值相同,也是连续排列:1,2,3,4·····
-- row_number()over(partition by 列名 order by 列名)
![cf92213e87a0e856f9de9f27658e689b.png](https://img-blog.csdnimg.cn/img_convert/cf92213e87a0e856f9de9f27658e689b.png)
【2】聚合窗口函数(不常用)
聚合函数:sum、avg、count、max、min
- sum作为窗口函数使用,计算累计值
select
![9dde12608ea50090b40875228e0d4a86.png](https://img-blog.csdnimg.cn/img_convert/9dde12608ea50090b40875228e0d4a86.png)
- avg作为窗口函数使用,计算移动平均
拓展:指定框架(汇总范围)
row N preceding :截止到当前记录之前N行——加上自身,一共计算N+1行值的平均值
row N following:截止到当前记录之后N行 ——加上自身,一共计算N+1行值的平均值
![86760c3be2149a23dedd17fa0cdadeac.png](https://img-blog.csdnimg.cn/img_convert/86760c3be2149a23dedd17fa0cdadeac.png)
SELECT
![a95a85224092757b877b4275518bc8f9.png](https://img-blog.csdnimg.cn/img_convert/a95a85224092757b877b4275518bc8f9.png)
六、自连接
自连接查询就是以类似多表对比的方式,实现对同一张表内的数据进行复杂的关系表示或关系处理。
示例1:成绩表score,查询001课程成绩>= 002课程成绩的学生的学号
![23372ef31c2aea27aeb27146da479293.png](https://img-blog.csdnimg.cn/img_convert/23372ef31c2aea27aeb27146da479293.png)
写法一:
SELECT
写法二:
SELECT
![5e98d123c4aa66f6dbc517c48b7e1534.png](https://img-blog.csdnimg.cn/img_convert/5e98d123c4aa66f6dbc517c48b7e1534.png)
示例2:weather表,查询今日温度比昨天温度高的日期
![dda9216070c63a466c2a96d56677d2e2.png](https://img-blog.csdnimg.cn/img_convert/dda9216070c63a466c2a96d56677d2e2.png)
写法一:
select
写法二:
select