这个link将显示我的架构和所涉及的所有表的内容。
我的目标是,使用单个Select语句,显示每个艺术家的名称,并按照他们所拥有的“摇滚”歌曲的数量排序,即使对于没有任何艺术家的艺术家,也按照最少的顺序排列。这是我尝试过的,显然没有用。
SELECT
Musical_genre.musical_genre_id,
COUNT(Musical_genre.musical_genre_id) AS nr_rocksongs
FROM
Musical_genre
JOIN
Album ON Album.musical_genre_id = Musical_genre.musical_genre_id
JOIN
Recording_artist ON Album.recording_artist_id = Recording_artist.recording_artist_id
GROUP BY
Album.recording_artist_id, Recording_artist.artist_name, Musical_genre.musical_genre_id
ORDER BY
nr_rocksongs ASC
我想要的任何想法都能得到我想要的结果吗?非常感谢所有帮助。
答案 0 :(得分:1)
从group by中删除Musical_genre.musical_genre_id并在select
中添加artist_nameSELECT Musical_genre.musical_genre_id, Recording_artist.artist_name,
COUNT(Musical_genre.musical_genre_id) AS nr_rocksongs
FROM Musical_genre
JOIN Album ON Album.musical_genre_id = Musical_genre.musical_genre_id
JOIN Recording_artist ON Album.recording_artist_id = Recording_artist.recording_artist_id
GROUP BY Musical_genre.musical_genre_id, Recording_artist.artist_name
ORDER BY nr_rocksongs ASC
答案 1 :(得分:0)
你需要从艺术家开始,然后加入专辑和流派。
SELECT
ra.artist_name,
mg.musical_genre_id,
mg.musical_genre,
COUNT(mg.musical_genre_id) AS nr_rocksongs
FROM
Recording_artist ra
LEFT JOIN Album a ON
a.recording_artist_id = ra.recording_artist_id
LEFT JOIN Musical_genre mg ON
a.musical_genre_id = mg.musical_genre_id
AND mg.musical_genre = 'Rock'
--use musical_genre_id = 201 if you like, but then you could remove the last left join entirely.
LEFT JOIN Song s ON
a.album_id = s.album_id
GROUP BY
mg.musical_genre_id,
mg.musical_genre,
ra.artist_name
ORDER BY
nr_rocksongs ASC