我必须执行SQL查询才能根据视图数量获得前10个专辑。视图数实际上是指定相册中每首歌曲的视图总和。
我的表是:
albums:
- album_id
- album_name
- album_owner
songs:
- song_id
- song_name
- song_album
- song_owner
- song_views
你能帮我解决这个问题吗?
答案 0 :(得分:4)
select sum(song_views) as 'song_sum',album_name
from albums a
inner join
songs s
on a.album_id = s.song_album
group by album_name
order by song_sum DESC
limit 0,10;
如果song_album指的是专辑ID ...
答案 1 :(得分:3)
试试这个:
SELECT TOP 10 a.album_id, a.album_name, a.album_owner, SUM(s.song_views)
FROM albums a
INNER JOIN
songs s
ON a.album_id = s.song_album
GROUP BY a.album_id, a.album_name, a.album_owner
ORDER BY SUM(s.song_views) DESC
答案 2 :(得分:2)
类似的东西:
select top 10 song_album
from songs
group by song_album
order by sum(song_views) desc
答案 3 :(得分:0)
我无法运行,但它应该是
select album_name, sum(song_views) as views from albums join songs on songs.album_id = songs.song_album group by album_id order by views desc limit 10
答案 4 :(得分:0)
SELECT TOP 10 song_album FROM songs GROUP BY song_album ORDER BY sum(song_views)desc