选择包含最多特定条目的列表

时间:2017-03-22 22:25:19

标签: mysql database

我有以下数据库

enter image description here

我想选择包含大多数AC / DC歌曲的播放列表,但我的脚本不起作用。

我的剧本:

SELECT playlist.Name
FROM playlist
INNER JOIN playlisttrack
ON playlist.PlaylistId = playlisttrack.PlaylistId
INNER JOIN track
ON playlisttrack.TrackId = track.TrackId
INNER JOIN album
ON track.AlbumId = album.AlbumId
WHERE ArtistId = 1;

1 个答案:

答案 0 :(得分:0)

自从我完成MySql以来已经有一段时间了,但它应该非常接近这一点。

SELECT playlist.Name
FROM playlist
INNER JOIN playlisttrack
ON playlist.PlaylistId = playlisttrack.PlaylistId
INNER JOIN track
ON playlisttrack.TrackId = track.TrackId
INNER JOIN album
ON track.AlbumId = album.AlbumId
WHERE album.ArtistId = 1
GROUP BY playlist.PlayListId, playlist.Name
ORDER BY COUNT(*) DESC
LIMIT 1;

请注意,Group By将您的播放列表混为一谈(我包括PlayListId,因为您没有做任何表示您的姓名是唯一的)

我正在ORDER BY COUNT(*) DESC进行每个播放列表中的计数,然后我执行LIMIT 1来获得第一个。