Mysql8.0的新特点with声明
https://dev.mysql.com/doc/refman/8.0/en/with.html
也叫做common table expression。(CTE)
CTE是一个命名的临时结果集合,用在一个声明的内部,可以被多次反复使用。相当于一个块。
本文只是简单的介绍一下。
CTE的用途就是优化查询sql的方法。见这篇文章:Section 8.2.2.4, “Optimizing Derived Tables, View References, and Common Table Expressions with Merging or Materialization”.
Common Table Expressions
使用with子句和1个或几个逗号分隔的子句。每个子句提供一个子查询,子查询产生一个结果集合,给这个集合设置一个名字。例子:
mysql> with -> sc_60 as (select * from SC where score >= 60) -> select * from sc_60; +------+------+-------+ | SId | CId | score | +------+------+-------+ | 01 | 01 | 80.0 | | 01 | 02 | 90.0 | | 01 | 03 | 99.0 | | 02 | 01 | 70.0 | | 02 | 02 | 60.0 | | 02 | 03 | 80.0 | | 03 | 01 | 80.0 | | 03 | 02 | 80.0 | | 03 | 03 | 80.0 | | 05 | 01 | 76.0 | | 05 | 02 | 87.0 | | 07 | 02 | 89.0 | | 07 | 03 | 98.0 | +------+------+-------+ 13 rows in set (0.00 sec)