SQL 处理顺序
SQL Order of processing
假设我有一个类似
的查询
select * from A
Except
select * from B
union all
select * from B
except
select * from A
查询的处理方式与
相同
select *
from
(
select * from A
Except
select * from B
) a
union all
(
select * from B
Except
select * from A
) b
sql中定义的处理顺序是怎样的?无论如何都会这样处理
select * from A
Except
select * from
(
select * from B
union all
select * from B
) a
except
select * from A
处理 EXCEPT 和 UNION "left to right"。这意味着没有任何括号来确定它们将按照它们在 sql 中出现的顺序进行处理。
UNION
和 EXCEPT
具有 相同的优先级 但将从左到右绑定,这意味着它们按“从左到右”的顺序进行评估,如他们被处理了。
来自@SeanLange 的 URL (TL;DR),值得注意的是:
If EXCEPT or INTERSECT is used together with other operators in an
expression, it is evaluated in the context of the following
precedence:
- Expressions in parentheses
- The INTERSECT operator
- EXCEPT and UNION evaluated from left to right based on their position in the
expression
假设我有一个类似
的查询select * from A
Except
select * from B
union all
select * from B
except
select * from A
查询的处理方式与
相同select *
from
(
select * from A
Except
select * from B
) a
union all
(
select * from B
Except
select * from A
) b
sql中定义的处理顺序是怎样的?无论如何都会这样处理
select * from A
Except
select * from
(
select * from B
union all
select * from B
) a
except
select * from A
处理 EXCEPT 和 UNION "left to right"。这意味着没有任何括号来确定它们将按照它们在 sql 中出现的顺序进行处理。
UNION
和 EXCEPT
具有 相同的优先级 但将从左到右绑定,这意味着它们按“从左到右”的顺序进行评估,如他们被处理了。
来自@SeanLange 的 URL (TL;DR),值得注意的是:
If EXCEPT or INTERSECT is used together with other operators in an expression, it is evaluated in the context of the following precedence:
- Expressions in parentheses
- The INTERSECT operator
- EXCEPT and UNION evaluated from left to right based on their position in the expression