Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select persons who like apple and banana both

How to select people like apple and banana both from the data below?

table: MyTable

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

i.e in this case, P1, P2 should be the result.

I tried with

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

This is also resulting P3 because P3 also have apple.

Thanks for any help.

like image 334
Fahad Khan Avatar asked Dec 23 '14 08:12

Fahad Khan


1 Answers

SELECT a.persons 
FROM MyTable a JOIN MyTable b on a.persons=b.persons 
WHERE a.fruit='Apple' and b.fruit='Banana'
like image 123
Matt Healy Avatar answered Sep 29 '22 10:09

Matt Healy