SQL Server如何解释此where子句?

问题描述 投票:0回答:2

我正在查看其他人的生产代码,这让where子句达到预期的结果感到惊讶:

where C1 = 'one' and C2 not in ('apple','pear') and C3 <> 'pie'
   or C1 = 'two' and C2 not in ('apple','pear')             

C1实际上只有两个值,因此不必使用OR来指定两个值。无论如何,我认为它应该像这样:

where (C1 = 'one' and C2 not in ('apple','pear') and C3 <> 'pie')
   or (C1 = 'two' and C2 not in ('apple','pear'))

SQL Server将第一个语句解释为与带括号的第二个语句相同吗?如果没有,它将如何解释第一条陈述?

[我正在查看其他人的生产代码,这让where子句达到预期的结果感到惊讶:C1 ='one',而C2 not in('apple','pear')and C3 <>'pie '或C1 ='...

sql sql-server select
2个回答
1
投票

SQL Server在AND之前先评估OR,因此按您的建议添加括号不会影响结果。但是,附加的括号can


0
投票

这等于您的第一个查询。


0
投票

这很令人困惑-但this article对此进行了解释。

© www.soinside.com 2019 - 2024. All rights reserved.