[英]MySQL Query for selecting values based on 2 columns
I have a table with 4 columns: id, A, B, C. I need to select A based on B and C. 我有一个包含4列的表格:id,A,B,C。我需要根据B和C选择A。
B is a filter and has integer values, C are the filter options, also integer. B是一个过滤器,具有整数值,C是过滤器选项,也是整数。
I tried this: 我尝试了这个:
SELECT a FROM table WHERE (b=1 AND c IN (5,6,7)) AND (b=2 AND c IN (9,10,11))
But that doesn't work because B can't be 1 and 2 at the same time. 但这不起作用,因为B不能同时为1和2。 I tried with OR:
我尝试使用OR:
SELECT a FROM table WHERE (b=1 AND c IN (5,6,7)) OR (b=2 AND c IN (9,10,11))
This returns A but wrong... I need both conditions to be correct. 这将返回A,但不正确...我需要两个条件都正确。
How can I make this query work? 如何使该查询起作用?
Thank you! 谢谢!
Group by a
and then you can check the conditions in the having
clause 按
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.