MySQL公司和部门关系,MySQL关系部门

SQL查询:找到供应所有最爱啤酒的餐厅
博客内容涉及一个SQL查询问题,作者需要找出哪些人的最喜欢的啤酒都在某餐厅有库存。他们提供了两个表格:Favoritebeer(姓名、姓氏、啤酒名)和OnStock(啤酒名、餐厅、数量)。解决方案是一个复杂的SQL查询,通过连接两个表格并使用GROUP BY和HAVING子句来确保每个人的所有最爱啤酒都在同一餐厅供应。查询结果成功地列出了这些人的名字和姓氏。

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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值