[英]MySQL get all rows based on a specific column value (filter by column)
我在這里有此表A:
| colA | colB
| 1 | 2
| 2 | 2
| 3 | 3
| 3 | 2
我想查詢colB
為3和2的所有行。結果將是:
|colA | colB
| 3 | 3
| 3 | 2
我嘗試查詢:
select * from A where colB = 2 AND colB= 3
但是我一直都沒有回應。 嘗試過OR
但也無法正常工作。 我了解到,我嘗試過的是某種“行”過濾。
有沒有辦法處理“列”過濾?
嘗試這個:
select *
from A
where colA IN (select colA
from A
where colB IN (2, 3)
group by colA
having count(distinct colB) = 2)
或使用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
您可以使用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.