mysql 5 表选择查询
我有 5 个表:
- users
- Bands_users
- Bands
- Band_gigs
- gigs
一个用户可以有很多乐队,一个乐队可以有很多用户 一个演出可以有很多乐队,一个乐队可以有很多演出
给定用户 id 1,我想返回所有演出及其关联乐队。
任何帮助将不胜感激。
I have 5 tables:
- users
- bands_users
- bands
- bands_gigs
- gigs
A user can have many bands, a band can have many users
A gig can have many bands, a band can have many gigs
Given the user id 1, I would like to return all the gigs with their associated band.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西?
something like this?
您可以在一行中完成此操作,但从简单开始,基本思想是首先将用户与 Bands_users 和 Bands 连接,其中用户 id 位于 users 和 Bands_users 中,并且 Band id 位于 Bands_users 和 Bands 中。这将为您提供用户所在乐队的列表。然后,您将乐队与 band_gigs 和 gigs 一起加入,其中乐队 id 既在 Bands 又在 Bands_gigs 中,而演出 ID 也在 Bands_gigs 和 gigs 中。现在您已获得用户所在乐队列表的演出列表。
You can do this in one line, but to start simple, the basic idea is that you first join users with the bands_users and bands where the user id is in both users and bands_users, and the band id is in both bands_users and bands. That will get you the list of bands a user is in. Then you join bands with bands_gigs and gigs where the band id is in both bands and bands_gigs, and the gig id is in both bands_gigs and gigs. Now you have the list of gigs for the list of bands that a user is in.
可以从以下几点开始:
Something to start with: