我有一个包含4列的表:id,A,B,C。我需要根据B和C选择A.
B是一个过滤器,有整数值,C是过滤器选项,也是整数。
我试过了:
SELECT a FROM table WHERE (b=1 AND c IN (5,6,7)) AND (b=2 AND c IN (9,10,11))
但这不起作用,因为B不能同时为1和2。我尝试了OR:
SELECT a FROM table WHERE (b=1 AND c IN (5,6,7)) OR (b=2 AND c IN (9,10,11))
这会返回A但错误...我需要两个条件才能正确。
如何使此查询有效?
谢谢!
答案 0 :(得分:1)
按a
分组,然后您可以检查having
子句
SELECT a
FROM your_table
GROUP BY a
HAVING sum(b=1 AND c IN (5,6,7)) > 0
AND sum(b=2 AND c IN (9,10,11)) > 0
答案 1 :(得分:0)
SELECT a
FROM your_table
WHERE
CASE
WHEN b=1 THEN C IN(5,6,7)
WHEN b=2 THEN C IN(8,9,10)
END