Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select multiple values from column

Tags:

select

mysql

Here is the table

ID    WHO    FRUIT
1     Adam   Apple
2     Adam   Lemon
3     Eve    Apple
4     Adam   Grape
5     God    Papaya
6     Eve    Melon

How do I get all persons who have apple and lemon: in this case, so that I get the result Adam?

Furthermore, I want all persons who have apple and lemon or melon, so I would get Adam and Eve?

like image 859
proker Avatar asked Feb 10 '26 08:02

proker


1 Answers

Use a self join on the table.

First one:

SELECT t1.who
FROM   table t1
JOIN   table t2
ON     t1.who = t2.who
WHERE
       t1.fruit = 'Apple'
AND    t2.fruit = 'Lemon'

Second one:

SELECT t1.who
FROM   table t1
JOIN   table t2
ON     t1.who = t2.who
WHERE
       t1.fruit = 'Apple'
AND    ( t2.fruit = 'Lemon' OR t2.fruit = 'Melon' )
like image 164
martin clayton Avatar answered Feb 13 '26 09:02

martin clayton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!