概述

MySql 8新增了CTE(公共表达式)和窗口函数功能,这两个极大的简化了我们复杂查询的写法特别是一些分析类查询的写法。这两个函数极大的提高了我们复杂SQL可读性,使我们的代码后期更易于维护。熟悉数据库的同学,对Count(),Sum(),Max/Min等基本统计函数并不陌生。普通统计函数在每个分组内只能返回一条记录。而新增加的窗口函数和CTE函数可以一个分组返回多条函数。下面我们通过具体的例子来演示这两种函数的具体用法

窗口函数

例1:

. 有一张表结构如下

  1. mysql> desc mytest;
  2. +--------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +--------+---------------+------+-----+---------+-------+
  5. | id | int(11) | YES | | NULL | |
  6. | name | text | YES | | NULL | |
  7. | course | text | YES | | NULL | |
  8. | score | decimal(10,0) | YES | | NULL | |
  9. +--------+---------------+------+-----+---------+-------+
  • id是学号
  • name是学生姓名
  • course是学科名称
  • score是成绩

. 它有以下数据:

  1. mysql> select * from mytest;
  2. +------+------+----------+-------+
  3. | id | name | course | score |
  4. +------+------+----------+-------+
  5. | 1 | TOM | Math | 100 |
  6. | 2 | TOM | Letter | 90 |
  7. | 3 | TOM | English | 80 |
  8. | 4 | TOM | Physical | 100 |
  9. | 5 | TOM | Chemical | 99 |
  10. | 6 | CICI | Math | 90 |
  11. | 7 | CICI | Letter | 100 |
  12. | 8 | CICI | English | 80 |
  13. | 9 | CICI | Physical | 90 |
  14. | 10 | CICI | Chemical | 70 |
  15. | 11 | CoCo | Math | 90 |
  16. | 12 | CoCo | Letter | 92 |
  17. | 13 | CoCo | English | 93 |
  18. | 14 | CoCo | Physical | 94 |
  19. | 15 | CoCo | Chemical | 95 |
  20. +------+------+----------+-------+
  21. 15 rows in set (0.00 sec)

问题1:要查询出每一科最高分学生的学号,姓名,成绩和科目。在MySQL8之前主要是通过下面的方式来实现。

答案1:MySql 8之前的方法

  1. mysql>select x.id,x.name,x.course,x.score
  2. from ( select t.*,if(@_course=t.course ,@rn:=@rn+1,@rn:=1) as rn ,@_course:=t.course as _course
  3. from
  4. (select t.* from mytest t order by course,score desc ) t ,
  5. (select @rn:=0 rn ,@_course:='') b
  6. )x
  7. WHERE rn=1 ORDER BY course;
  8. +------+------+----------+-------+
  9. | id | name | course | score |
  10. +------+------+----------+-------+
  11. | 5 | TOM | Chemical | 99 |
  12. | 13 | CoCo | English | 93 |
  13. | 7 | CICI | Letter | 100 |
  14. | 1 | TOM | Math | 100 |
  15. | 4 | TOM | Physical | 100 |
  16. +------+------+----------+-------+
  17. 5 rows in set, 5 warnings (0.00 sec)

这个sql虽然高效,但是可读性不强。接下来我们来看看窗口函数怎么实现

答案2:使用窗口函数ROW_NUMBER

  1. mysql> select id,name,course,score
  2. from (
  3. select
  4. row_number()over(partition by course order by score desc) as rn,
  5. id,
  6. name,
  7. course,
  8. score
  9. from mytest
  10. )t where rn=1;
  11. +------+------+----------+-------+
  12. | id | name | course | score |
  13. +------+------+----------+-------+
  14. | 5 | TOM | Chemical | 99 |
  15. | 13 | CoCo | English | 93 |
  16. | 7 | CICI | Letter | 100 |
  17. | 1 | TOM | Math | 100 |
  18. | 4 | TOM | Physical | 100 |
  19. +------+------+----------+-------+

答案3 使用CTE方式

  1. mysql> with cte as(
  2. select row_number()over(partition by course order by score desc) as rn, id,name,course,score from mytest
  3. )
  4. select id,name,course,score from cte where rn = 1;
  5. +------+------+----------+-------+
  6. | id | name | course | score |
  7. +------+------+----------+-------+
  8. | 5 | TOM | Chemical | 99 |
  9. | 13 | CoCo | English | 93 |
  10. | 7 | CICI | Letter | 100 |
  11. | 1 | TOM | Math | 100 |
  12. | 4 | TOM | Physical | 100 |
  13. +------+------+----------+-------+
  14. 5 rows in set (0.00 sec)

通过对比上面的三种答案,发现CTE和窗口函数极大简化了我们的sql语句。我们的sql更容易读懂。

结论:

本文通过一个简单的例子介绍了MySQL新增的CTE和窗口函数来简化我们SQL开发的难度。关于具体的CTE和窗口函数的具体语法请查阅Mysql的官方文档。

原文:http://mysql.taobao.org/monthly/2018/11/09/