连接3个表SQL

发布于 2025-01-01 03:48:15 字数 596 浏览 0 评论 0原文

我尝试解决这个问题很长一段时间,但输出是错误的。谁能帮我解决这个问题吗?我使用的是mysql,所以全连接不起作用。谢谢你们

3 张桌子:

Frequents (attributes: drinker, bar, times_a_week),
Likes (attributes: drinker, beer),
Serves (attributes: bar, beer, price),

这个问题是针对所有经常光顾“每个”酒吧的饮酒者提出的,这些酒吧提供他们喜欢的啤酒

我的答案如下:

SELECT drinker 
FROM frequents 
WHERE drinker NOT IN (SELECT f.drinker FROM frequents f 
                  JOIN likes l ON f.drinker=l.drinker
                  LEFT JOIN serves s ON l.beer=s.beer
                  AND s.bar=f.bar
                  WHERE s.bar IS NULL)

I am trying to solve this problem for quite some time and the output is wrong. Can anyone help me with this? I am using mysql, so full join doesnt work. Thank yyou

3 tables:

Frequents (attributes: drinker, bar, times_a_week),
Likes (attributes: drinker, beer),
Serves (attributes: bar, beer, price),

The question ask for all the drinkers who frequent "every" bar which serve some beers they like

My answer looks like this:

SELECT drinker 
FROM frequents 
WHERE drinker NOT IN (SELECT f.drinker FROM frequents f 
                  JOIN likes l ON f.drinker=l.drinker
                  LEFT JOIN serves s ON l.beer=s.beer
                  AND s.bar=f.bar
                  WHERE s.bar IS NULL)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-01-08 03:48:15

怎么样:

select * 
from Frequents
where bar in (select bar from Serves  S join Likes  L on S.beer=l.beer)

如果它不起作用,您可以发布一些数据示例吗?

how about this:

select * 
from Frequents
where bar in (select bar from Serves  S join Likes  L on S.beer=l.beer)

if it doesn't work, can you post some example of your data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文