0

大家好,我有一个相当简单的问题。

我想知道这之间有什么区别

SELECT * 
  FROM TABLE 
 WHERE A = 'xxx' 
   AND (B = 'xxx' OR C='xxx' OR D='xxx')

而这个查询

SELECT * 
  FROM TABLE 
 WHERE A = 'xxx' 
   AND B = 'xxx' 
    OR C='xxx' 
    OR D='xxx'

在底部查询中,它将返回 2 个结果,而顶部查询将返回零个结果。

4

2 回答 2

3
A = 'xxx' AND B = 'xxx' OR C='xxx' OR D='xxx'

相当于

(A = 'xxx' AND B = 'xxx') OR (C='xxx') OR (D='xxx')

感谢运营商的优先顺序

于 2013-07-24T16:29:01.830 回答
2

这个

SELECT * FROM TABLE
WHERE A = 'xxx' AND (B = 'xxx' OR C='xxx' OR D='xxx')

相当于这个

SELECT * FROM TABLE
WHERE A = 'xxx' AND B = 'xxx' OR 
      A = 'xxx' AND C = 'xxx' OR
      A = 'xxx' AND D = 'xxx'

您放入 WHERE 子句的逻辑遵循布尔表达式的规则。就像代数表达式一样,有一个顺序操作。一般来说,顺序是

  1. 不是
  2. 或者
于 2013-07-24T16:28:17.113 回答