MySQL 加入,扩展结果

MySQL joining , extending the result

我有两张桌子

Table name: place
Columns: placeid, name
Data: 1, My Favourite Donut Place

Table name: category
Columns: categoryid, name, placeid
Data: 1, Donuts, 1
      2, Coffee, 1
      3, Hot Chocolate, 1

我这样加入两个:

 select p.name as place, c.name as category from place p join category c on p.placeid=c.placeid

并且会得到这样的结果:

place                     category
My favourite donut place  Donuts
My favourite donut place  Coffee
My favourite donut place  Hot Chocolate

现在我想搜索供应咖啡的地方:

select p.name as place, c.name as category from place p join category c on p.placeid=c.placeid where c.name = 'Coffee'

结果如下:

place                     category
My favourite donut place  Coffee

但我仍然想显示供应咖啡的 "My favourite donut place" 的所有不同类别,即 "Donuts" 和 "Hot Chocolate"。

实现该目标的最佳方法是什么?

您可以再次将地点连接到类别以获得所有类别,而在这种情况下连接顺序并不重要,我发现这种顺序更清楚。

SELECT p.name AS place, placeCategories .name AS category 
FROM category AS filterCategory 
INNER JOIN place AS p ON filterCategory.placeid=p.placeid 
INNER JOIN category AS placeCategories ON p.placeid=placeCategories.placeid 
WHERE filterCategory.name = 'Coffee'