更新时间:2024-12-13 GMT+08:00
WITH
WITH子句定义查询子句的命名关系,可以展平嵌套查询或简化子查询语句。例如下面的查询语句是等价的:
SELECT name, maxprice FROM (SELECT mame, MAX(price) AS maxprice FROM fruit GROUP BY name) AS x; WITH x AS (SELECT name, MAX(price) AS maxprice FROM fruit GROUP BY name) SELECT name, maxprice FROM x;
多个子查询
with t1 as(select name,max(price) as maxprice from fruit group by name), t2 as(select name,avg(price) as avgprice from fruit group by name) select t1.*,t2.* from t1 join t2 on t1.name = t2.name;
WITH的链式形式
WITH x AS (SELECT a FROM t), y AS (SELECT a AS b FROM x), z AS (SELECT b AS c FROM y) SELECT c FROM z;
父主题: DQL 语法