mysql通过关联表从2个表中检索数据

发布于 2024-10-07 22:24:19 字数 450 浏览 2 评论 0原文

我有一个 MYSQL 数据库,有 3 个表:

  1. 艺术家(artist_id,name)
  2. 专辑(album_i,title) 第三个相关表:
  3. 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:

  1. artists(artist_id,name)
  2. albums (album_i,title)
    and a third correlation table:
  3. 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 技术交流群。

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

发布评论

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

评论(4

衣神在巴黎 2024-10-14 22:24:19
select
  artist_id,
  album_id,
  ...
from
  albums_artists
join albums on albums.id = albums_artists.album_id
join artists on artists.id = albums_artists.artist_id
select
  artist_id,
  album_id,
  ...
from
  albums_artists
join albums on albums.id = albums_artists.album_id
join artists on artists.id = albums_artists.artist_id
甜嗑 2024-10-14 22:24:19
SELECT ar.artist_id, ar.name, al.album_id, al.title
    FROM artists ar
        INNER JOIN albums_artists aa
            ON ar.artist_id = aa.artist_id
        INNER JOIN albums al
            ON aa.album_id = al.album_id
    ORDER BY ar.name, al.title

而且,只是为了好玩,如果有一位艺术家尚未发行任何专辑怎么办?

SELECT ar.artist_id, ar.name, al.album_id, al.title
    FROM artists ar
        LEFT JOIN albums_artists aa
            INNER JOIN albums al
                ON aa.album_id = al.album_id
            ON ar.artist_id = aa.artist_id
    ORDER BY ar.name, al.title
SELECT ar.artist_id, ar.name, al.album_id, al.title
    FROM artists ar
        INNER JOIN albums_artists aa
            ON ar.artist_id = aa.artist_id
        INNER JOIN albums al
            ON aa.album_id = al.album_id
    ORDER BY ar.name, al.title

And, just for fun, what if there's an artist who hasn't released any albums yet.

SELECT ar.artist_id, ar.name, al.album_id, al.title
    FROM artists ar
        LEFT JOIN albums_artists aa
            INNER JOIN albums al
                ON aa.album_id = al.album_id
            ON ar.artist_id = aa.artist_id
    ORDER BY ar.name, al.title
看透却不说透 2024-10-14 22:24:19
select a.artist_id, a.name, al.album_id, al.title
from artists a, albums al, albums_artists ala
where a.artist_id = ala.artist_id
and ala.album_id = al.album_id
order by ala.artist_id
select a.artist_id, a.name, al.album_id, al.title
from artists a, albums al, albums_artists ala
where a.artist_id = ala.artist_id
and ala.album_id = al.album_id
order by ala.artist_id
紫竹語嫣☆ 2024-10-14 22:24:19

像这样的东西:

select ar.artist_id, ar.name, al.album_id, al.title
  from artists ar
  join album_artists aa on aa.artist_id = ar.artist_id
  join albums al on al.album_id = aa.album_id and ar.artist_id = aa.artist_id
order by ar.artist_id

Something like this:

select ar.artist_id, ar.name, al.album_id, al.title
  from artists ar
  join album_artists aa on aa.artist_id = ar.artist_id
  join albums al on al.album_id = aa.album_id and ar.artist_id = aa.artist_id
order by ar.artist_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文