下面的查询会先执行内层,再执行外层。
SELECT
product_type,
cnt_product
FROM
(
SELECT
product_type,
COUNT(*) AS cnt_product
FROM
Product
GROUP BY
product_type
) AS ProductSum;
内层是
SELECT product_type, COUNT(*) AS cnt_product FROM Product
GROUP BY product_type;
外层是
SELECT product_type, cnt_product FROM ProductSum;
可认为,每个子查询都会形成一张新的表,每个外层查询都是从子查询,即内层查询生成的新的表中去操作数据。