MySQL从列中选择多个值

pro*_*ker 3 mysql select

这是表格

ID    WHO    FRUIT
1     Adam   Apple
2     Adam   Lemon
3     Eve    Apple
4     Adam   Grape
5     God    Papaya
6     Eve    Melon
Run Code Online (Sandbox Code Playgroud)

我如何得到所有拥有苹果和柠檬的人:在这种情况下,我得到结果亚当?

此外,我想要所有拥有苹果,柠檬或甜瓜的人,所以我会得到亚当和夏娃?

mar*_*ton 6

在表格上使用自联接.

第一:

SELECT t1.who
FROM   table t1
JOIN   table t2
ON     t1.who = t2.who
WHERE
       t1.fruit = 'Apple'
AND    t2.fruit = 'Lemon'
Run Code Online (Sandbox Code Playgroud)

第二个:

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' )
Run Code Online (Sandbox Code Playgroud)