mysql通过关联表从2个表中检索数据
我有一个 MYSQL 数据库,有 3 个表:
- 艺术家(artist_id,name)
- 专辑(album_i,title) 第三个相关表:
- albums_artists (album_id, Artist_id)
我的目标是显示所有艺术家的列表,并在每个艺术家的名字旁边显示该艺术家发行的所有专辑的标题。 (一张专辑“属于”仅一位艺术家,当然一位艺术家可能拥有不止一张专辑)
如果能够得到如下结果那就太好了:
1“ac/dc”3“let there be rock”
1“交流/直流”4“高压”
2“深紫”45“机头”
2“深紫色”49“岩石中”
3 “motorhead” 666 “黑桃 A” num
(每行中的第一个数字是艺术家 ID,第二个数字是专辑 ID)。
对此合适的查询是什么?
I have a MYSQL Db with 3 tables:
- artists(artist_id,name)
- albums (album_i,title)
and a third correlation table: - albums_artists (album_id, artist_id)
My goal is to display a list of all the artists and next to each artist's name, the titles of all the albums this artist released. (An album "belongs" to only one artist, an artist may have more than one album of course)
It would be great if a could get a result like:
1 "ac/dc" 3 "let there be rock"
1 "ac/dc" 4 "high voltage"
2 "deep purple" 45 "machine head"
2 "deep purple" 49 "in rock"
3 "motorhead" 666 "the ace of spades" num
(The first number in each row being the artist_id, and the second the album_id).
What would be the appropriate query for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而且,只是为了好玩,如果有一位艺术家尚未发行任何专辑怎么办?
And, just for fun, what if there's an artist who hasn't released any albums yet.
像这样的东西:
Something like this: