SQL逻辑运算符优先级:和和或 [英] SQL Logic Operator Precedence: And and Or
本文介绍了SQL逻辑运算符优先级:和和或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的两个语句是否相等?
Are the two statements below equivalent?
SELECT [...]
FROM [...]
WHERE some_col in (1,2,3,4,5) AND some_other_expr
和
SELECT [...]
FROM [...]
WHERE some_col in (1,2,3) or some_col in (4,5) AND some_other_expr
我可以使用某种真值表来验证这一点吗?
Is there some sort of truth table I could use to verify this?
推荐答案
And
的优先级高于Or
,因此,即使a <=> a1 Or a2
And
has precedence over Or
, so, even if a <=> a1 Or a2
Where a And b
与
Where a1 Or a2 And b,
因为将执行为
Where a1 Or (a2 And b)
以及要使它们相同的内容如下(使用括号覆盖优先级规则):
and what you want, to make them the same, is the following (using parentheses to override rules of precedence):
Where (a1 Or a2) And b
这里有一个例子来说明:
Here's an example to illustrate:
Declare @x tinyInt = 1
Declare @y tinyInt = 0
Declare @z tinyInt = 0
Select Case When @x=1 OR @y=1 And @z=1 Then 'T' Else 'F' End -- outputs T
Select Case When (@x=1 OR @y=1) And @z=1 Then 'T' Else 'F' End -- outputs F
对于那些喜欢查阅参考文献的人(按字母顺序):
For those who like to consult references (in alphabetic order):
- Microsoft Transact-SQL operator precedence
- Oracle MySQL 9 operator precedence
- Oracle 10g condition precedence
- PostgreSQL operator Precedence
- SQL as understood by SQLite
这篇关于SQL逻辑运算符优先级:和和或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文