在MySQL中将两个SELECT与不同的WHERE合并

时间:2013-03-17 13:27:44

标签: mysql

有没有办法合并这两个查询?

SELECT * FROM OFF WHERE New = 0 AND Saw = 1 AND El = 0
SELECT * FROM OFF WHERE New = 1 AND Saw = 0 AND El = 0

如果我使用此查询:

SELECT * FROM OFF WHERE New = 0 AND Saw = 1 AND El = 0 AND New = 1 AND Saw = 0 AND El = 0

我显然有其他人按预期结果。

谢谢!

2 个答案:

答案 0 :(得分:1)

在对这些条件进行分组时使用OR

SELECT * 
FROM   `OFF`
WHERE  (New = 0 AND Saw = 1 AND El = 0) OR 
       (New = 1 AND Saw = 0 AND El = 0)

答案 1 :(得分:0)

您始终可以使用UNION

合并任意两个查询
(SELECT * FROM OFF WHERE New = 0 AND Saw = 1 AND El = 0)
UNION
(SELECT * FROM OFF WHERE New = 1 AND Saw = 0 AND El = 0)