如下所示SQL语句展示了逻辑查询的处理顺序
(8) SELECT (9) DISTINCT (11)
(1) FROM
(3) JOIN
(2) ON
(4) WHERE
(5) GROUP BY
(6) WITH {CUBE | ROLLUP}
(7) HAVING
(10) ORDER BY
步骤1 FROM:查询语句中from语句中的两个表进行交叉连接,生成虚拟表V1
步骤2 ON:筛选器ON应用到V1上,并把满足条件的行插入虚拟表V2
步骤3 OUTER(join):如果SQL语句中选择了OUTER JOIN则将其余的行也插入V2,生成虚拟表V3,。如果join语句中存在多个table,则重复以上步骤。
步骤4 WHERE:根据条件筛选相应的行插入至虚拟表V4
步骤5 GROUP BY:分类V4并插入虚拟表V5
步骤6 CUBE | ROLLUP:增加超级组的行至V5,生成V6
步骤7 HAVING:根据条件筛选相应的行插入至虚拟表V7
步骤8 SELECT:生成SELECT内容,生成V8
步骤9 DISTINCT:去掉重复的行,生成V9
步骤10 ORDER BY:排序生成V10
步骤11 TOP:保留前几条记录并返回结果
我们可以通过一个具体实例说明问题
Table 1如下
CustomerID | City |
FISSA | Madrid |
FRNDO | MAdrid |
KRLOS | Madrid |
MRPHS | Zion |
Table 2如下
OrdID | CustomerID |
1 | FRNDO |
2 | FRNDO |
3 | KRLOS |
4 | KRLOS |
5 | KRLOS |
6 | MRPHS |
7 | NULL |
需要使用的query语句如下:
SELECT C.customerid, COUNT(O.orderid) AS numorders
FROM dbo.Customers AS C
LEFT OUTER JOIN dbo.Orders AS O
ON C.customerid = O.customerid
WHERE C.city = 'Madrid'
GROUP BY C.customerid
HAVING COUNT(O.orderid) < 3
ORDER BY numorders;
第一步,两个table连接并生产一个28行的虚拟表V1(4*7),如下
C.customerid | C.city | O.orderid | O.customerid |
FISSA | Madrid | 1 | FRNDO |
FISSA | Madrid | 2 | FRNDO |
FISSA | Madrid | 3 | KRLOS |
FISSA | Madrid | 4 | KRLOS |
FISSA | Madrid | 5 | KRLOS |
FISSA | Madrid | 6 | MRPHS |
FISSA | Madrid | 7 | NULL |
FRNDO | Madrid | 1 | FRNDO |
FRNDO | Madrid | 2 | FRNDO |
FRNDO | Madrid | 3 | KRLOS |
FRNDO | Madrid | 4 | KRLOS |
FRNDO | Madrid | 5 | KRLOS |
FRNDO | Madrid | 6 | MRPHS |
FRNDO | Madrid | 7 | NULL |
KRLOS | Madrid | 1 | FRNDO |
KRLOS | Madrid | 2 | FRNDO |
KRLOS | Madrid | 3 | KRLOS |
KRLOS | Madrid | 4 | KRLOS |
KRLOS | Madrid | 5 | KRLOS |
KRLOS | Madrid | 6 | MRPHS |
KRLOS | Madrid | 7 | NULL |
MRPHS | Zion | 1 | FRNDO |
MRPHS | Zion | 2 | FRNDO |
MRPHS | Zion | 3 | KRLOS |
MRPHS | Zion | 4 | KRLOS |
MRPHS | Zion | 5 | KRLOS |
MRPHS | Zion | 6 | MRPHS |
MRPHS | Zion | 7 | NULL |
第二步,判断ON的条件生产虚拟表V2,如下
C.customerid | C.city | O.orderid | O.customerid |
FRNDO | Madrid | 1 | FRNDO |
FRNDO | Madrid | 2 | FRNDO |
KRLOS | Madrid | 3 | KRLOS |
KRLOS | Madrid | 4 | KRLOS |
KRLOS | Madrid | 5 | KRLOS |
MRPHS | Zion | 6 | MRPHS |
第三步,添加OUTER JOIN的结果,如下
C.customerid | C.city | O.orderid | O.customerid |
FRNDO | Madrid | 1 | FRNDO |
FRNDO | Madrid | 2 | FRNDO |
KRLOS | Madrid | 3 | KRLOS |
KRLOS | Madrid | 4 | KRLOS |
KRLOS | Madrid | 5 | KRLOS |
MRPHS | Zion | 6 | MRPHS |
FISSA | Madrid | NULL | NULL |
第四步,执行WHERE条件判断,如下
C.customerid | C.city | O.orderid | O.customerid |
FRNDO | Madrid | 1 | FRNDO |
FRNDO | Madrid | 2 | FRNDO |
KRLOS | Madrid | 3 | KRLOS |
KRLOS | Madrid | 4 | KRLOS |
KRLOS | Madrid | 5 | KRLOS |
FISSA | Madrid | NULL | NULL |
第五步,执行GROUP排序,如下
C.customerid | C.customerid | C.city | O.orderid | O.customerid |
FRNDO | FRNDO | Madrid | 1 | FRNDO |
FRNDO | Madrid | 2 | FRNDO | |
KRLOS | KRLOS | Madrid | 3 | KRLOS |
KRLOS | Madrid | 4 | KRLOS | |
KRLOS | Madrid | 5 | KRLOS | |
FISSA | FISSA | Madrid | NULL | NULL |
第六步,本列中略过
第七步,执行HAVING条件,如下
C.customerid | C.customerid | C.city | O.orderid | O.customerid |
FRNDO | FRNDO | Madrid | 1 | FRNDO |
FRNDO | Madrid | 2 | FRNDO | |
FISSA | FISSA | Madrid | NULL | NULL |
第八步,执行SELECT,如下
C.customerid | numorders |
FRNDO | 2 |
FISSA | 0 |
第九步,执行DISTINCT
第十步,执行ORDER BY,如下
C.customerid | numorders |
FISSA | 0 |
FRNDO | 2 |
第十一步,执行TOP