2016-03-29 13 views
0

göre tüm satırları almak:MySQL belirli bir sütun değeri (kolon filtre) burada bu tablo A sahip

| colA | colB 
| 1 | 2 
| 2 | 2 
| 3 | 3 
| 3 | 2 

I colB tüm satırları sorgulamak istediğiniz hem 3 hem de 2. olan sonuç olarak olacaktır:

|colA | colB 
| 3 | 3 
| 3 | 2 

Ben sorgulama çalıştı:

select * from A where colB = 2 AND colB= 3 

Ama boş bir yanıt alıyorum. OR denedim ama aynı zamanda işe yaramayacak. Denediğimin, bir "satır" filtrelemesi olduğunu anlıyorum.

"Sütun" filtrelemeyi işlemenin bir yolu var mı?

select * 
from A 
where colA IN (select colA 
       from A 
       where colB IN (2, 3) 
       group by colA 
       having count(distinct colB) = 2) 

veya JOIN bir ile:

cevap

1

bu deneyin

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 
+0

Sorumu güncelledik. hehe Karışmaktan dolayı açıklığa kavuşturmak isterim, bu benim güncellenmiş sorumu ele alıyor mu? Teşekkürler! – Jhn

+0

@Jhn Evet, güncellenen soruyu ele alıyor. –

+0

Hızlı soru, neden sayısı = 2? – Jhn

1

Sen EXISTS() kullanabilirsiniz:

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) 
İlgili konular