2014-12-23 26 views
6

Come selezionare persone come la mela e la banana dai dati seguenti?seleziona persone a cui piacciono sia la mela che la banana

tabella: MyTable

persons | fruit 
----------------------------- 
    P1  Apple 
    P1  Banana 
    P1  Mango 
    P2  Banana 
    P2  Apple 
    P3  Mango 
    P3  Apple 

cioè in questo caso, P1, P2 deve essere il risultato.

ho provato con

select * from MyTable where fruit in("Apple","Banana"); 

Questo è anche traduce P3 perché P3 hanno anche mela.

Grazie per qualsiasi aiuto.

+3

'gruppo da parte di persone che hanno COUNT (*) = 2' –

+0

P1 risultante, Apple – Fahad

+0

Ha funzionato Grazie. – Fahad

risposta

4
SELECT a.persons 
FROM MyTable a JOIN MyTable b on a.persons=b.persons 
WHERE a.fruit='Apple' and b.fruit='Banana' 
1

Prova questo:

SELECT persons 
FROM MyTable 
WHERE fruit IN ('Apple', 'Banana') 
GROUP BY persons 
HAVING COUNT(DISTINCT fruit) = 2; 
+0

Il suo P1 risultante, Apple @Saharsh – Fahad

0
select * from MyTable where fruit in("Apple") and persons in(select persons from MyTable where fruit in("Banana"); 
1

questo dovrebbe funzionare:

SELECT distinct `t1`.`persons` FROM MyTable AS `t1` 
INNER JOIN MyTable AS `t2` ON `t1`.`persons` = `t2`.`persons` 
WHERE `t1`.`fruit` = 'Banana' AND `t2`.`fruit` = 'Apple' 
-1

Prova questo:

select persons from MyTable where fruit in("Apple","Banana");