我有一个包含多个字段的表,我想选择两个字段等于某个值的所有行,其中field3为1,field4为4,5,6或field3为0.但是我需要返回结果field3为0,field4为4,5,6。使用此查询,我没有使用field3 = 0返回行。
这是我的查询,
SELECT * from table where field1 = 1 AND field2 = 0 AND (field3 = 1 AND field4 !=4
AND field4 !=5
AND field4 !=6)
答案 0 :(得分:2)
尝试这样的事情:
SELECT * from table where
field1 = 1 AND field2 = 0
AND (
(field3 = 1 AND (field4 = 4 OR field4 = 5 OR field4 = 6))
OR
(field3 = 0)
)
答案 1 :(得分:1)
根据你的散文文本对你的陈述进行聚类和括号,你就在那里。
SELECT *
FROM
table
WHERE
field1 = x
AND field2 = y
AND (
(field3 = 1 AND field4 IN (4, 5, 6)) -- field3 = 1 and field4 has one of specified values
OR -- or alternatively
(field3 = 0 AND field4 NOT IN (4, 5, 6)) -- field3 = 0 and field4 doesn't have any of the specified values
)
修改强> 实际上 - 再次阅读你的帖子后,你想要field4为4,5或6你想让field3为0或1 - 或者我错了? 这意味着:
SELECT *
FROM
table
WHERE
field1 = x
AND field2 = y
AND field3 IN (0, 1)
AND field4 IN (4, 5, 6))