我有一张餐桌:
NAME | FOOD
-----------------
matthew | rice
matthew | beans
mark | rice
mark | beans
Luke | rice
john | beans
我需要提取只有大米的名称、只有豆类的名称以及两者兼有的名称。如果它们位于单独的表中,则 LEFT JOIN 将起作用。但我遇到了困难,因为他们在一张桌子上。
我已经尝试了这两个语句的变体,但没有运气:
SELECT name
FROM diners
WHERE NOT EXISTS
(SELECT name
FROM diners
WHERE food = 'beans')
SELECT t1.name FROM diners AS t1
LEFT JOIN diners AS t2 ON t1.name = t2.name
WHERE t2.food = 'rice'
AND t2.name IS NULL