Mysql8.0 of new features: with a statement

Mysql8.0 of new features with the statement

https://dev.mysql.com/doc/refman/8.0/en/with.html

Also known as common table expression. (CTE)

CTE is a temporary named result set, used inside a declaration, can be used repeatedly. Equivalent to a block.

 

This article is a brief look.

CTE is to optimize the use of the sql query method. See this article: Section 8.2.2.4, "Optimizing Derived the Tables, the References View, and the Common or Materialization Merging with the Table Expressions" .

 

Common Table Expressions

With the use clause and one or several comma-separated clause. Each sub-clause provides a query, sub query produces a result set, set a name for this collection. example:

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)

Guess you like

Origin www.cnblogs.com/chentianwei/p/12145280.html