[英]MySQL get all rows based on a specific column value (filter by column)
I have this table A here: 我在这里有此表A:
| colA | colB
| 1 | 2
| 2 | 2
| 3 | 3
| 3 | 2
I want to query all rows where colB
is both 3 and 2. In which the result would be: 我想查询colB
为3和2的所有行。结果将是:
|colA | colB
| 3 | 3
| 3 | 2
I tried querying: 我尝试查询:
select * from A where colB = 2 AND colB= 3
But I keep getting an empty response. 但是我一直都没有回应。 Tried OR
but it also won't work. 尝试过OR
但也无法正常工作。 I understand that what I experimented is sort of a "row" filtering. 我了解到,我尝试过的是某种“行”过滤。
Is there a way to handle "column" filtering? 有没有办法处理“列”过滤?
Try this: 尝试这个:
select *
from A
where colA IN (select colA
from A
where colB IN (2, 3)
group by colA
having count(distinct colB) = 2)
or with a JOIN
: 或使用JOIN
:
select A.*
from A
join (select colA
from A
where colB IN (2, 3)
group by colA
having count(distinct colB) = 2
) as t on A.colA = t.ColA
You can use EXISTS()
: 您可以使用EXISTS()
:
SELECT * FROM YourTable a
WHERE EXISTS(SELECT 1 from YourTable s
WHERE a.colA = s.colA
and s.colB in(2,3)
GROUP BY s.colA having count(distinct s.colB) = 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.