我正在查看其他人的生产代码,这让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 Server在AND
之前先评估OR
,因此按您的建议添加括号不会影响结果。但是,附加的括号can
这等于您的第一个查询。
这很令人困惑-但this article对此进行了解释。