1

是否可以OR在 SQL 查询中包含一条语句而不必重复不变的信息?以此为例:

SELECT *
FROM table
WHERE a = 1
AND b = 1
AND c = 1
AND x = 1
AND y = 2
OR a = 1
AND b = 1
AND c = 1
AND x = 2
AND y = 1

这是我能弄清楚如何做到这一点的唯一方法。a、b 和 c 不会改变,所以我认为当它们保持不变时必须重复它们是不合逻辑的。OR有什么方法可以避免在包含语句时重复不改变的值?

4

3 回答 3

4
SELECT * FROM table
WHERE a = 1 AND b = 1 AND c = 1
AND 
( 
   (x = 1 AND y = 2) OR (x = 2 AND y = 1)
)
于 2013-09-19T09:24:12.557 回答
4
WHERE  a = 1
AND    b = 1
AND    c = 1
AND    (
         ( x = 1 AND y = 2 )
       OR
         ( x = 2 AND y = 1 )
       )

替代选项(更清晰的 IMO):

SELECT *
FROM   (
        SELECT *
        FROM   table
        WHERE  a = 1
        AND    b = 1
        AND    c = 1
       ) As a_subquery
WHERE  (x = 1 AND y = 2)
    OR (x = 2 AND y = 1)
于 2013-09-19T09:24:42.867 回答
2

您可以使用括号:

SELECT *
FROM table
WHERE a = 1
AND b = 1
AND c = 1
AND (x = 1 AND y=2
     OR x=2 AND y = 1)
于 2013-09-19T09:26:35.190 回答