I am having difficulties to solve one exercise:
For which People there is a Restaurant, that serves ALL their favorite beers.
(Yes, we actually have this in school :D)
I have got 2 Tables that can be used:
Table1: Favoritebeer (Name, Surname, beername)
Table2: OnStock (beername, restaurant, quantity)
My solution would be: OnStock % Favoritebeer
There is no such thing like DIVISION in MySQL. Any ideas how I could solve that? I found the following on Wikipedia: http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29 which is exactly what I need but I am having difficulties to translate it in SQL.
EDIT:
The result should be:
Bucher Rolf
Mastroyanni Pepe
Meier Hans
Meier Hanspeter
Meier Hansruedi
Müller Heinrich
Peters Peter
Zarro Darween
解决方案
Give this a try:
SELECT DISTINCT fb1.name, fb1.surname FROM favoriteBeer fb1
JOIN stock s ON fb1.beerName = s.beerName
GROUP BY fb1.name, fb1.surname, s.restaurant
HAVING COUNT(*) = (
SELECT COUNT(*) FROM favoriteBeer fb2
WHERE fb1.name = fb2.name AND fb1.surname = fb2.surname
)
Output:
| NAME | SURNAME |
|-------------|-----------|
| Bucher | Rolf |
| Mastroyanni | Pepe |
| Meier | Hans |
| Meier | Hanspeter |
| Meier | Hansruedi |
| Müller | Heinrich |
| Peters | Peter |
| Zarro | Darween |
Fiddle here.
SQL查询:找到供应所有最爱啤酒的餐厅
博客内容涉及一个SQL查询问题,作者需要找出哪些人的最喜欢的啤酒都在某餐厅有库存。他们提供了两个表格:Favoritebeer(姓名、姓氏、啤酒名)和OnStock(啤酒名、餐厅、数量)。解决方案是一个复杂的SQL查询,通过连接两个表格并使用GROUP BY和HAVING子句来确保每个人的所有最爱啤酒都在同一餐厅供应。查询结果成功地列出了这些人的名字和姓氏。

被折叠的 条评论
为什么被折叠?



