0

我试图制作一个图表列表,我使用以下查询

SELECT 
    vmm_user.username, 
    vmm_songs.*, 
    vmm_albums.desc, 
    vmm_albums.release, 
    vmm_albums.name, 
    AVG(vmm_songrating.rating) AS ratingavg,
    COUNT(vmm_songrating.id) AS ratingcount
FROM 
    vmm_songs 
LEFT JOIN 
    vmm_user 
ON 
    vmm_songs.userid=vmm_user.id 
LEFT JOIN 
    vmm_albums 
ON 
    vmm_songs.albumid=vmm_albums.id 
LEFT JOIN  
    vmm_songrating  
ON 
    vmm_songs.id=vmm_songrating.songid 
GROUP BY 
    vmm_songs.id
HAVING 
    COUNT(vmm_songrating.id) >= 2 
ORDER BY
    AVG(vmm_songrating.rating) DESC
LIMIT 
    10

正如你所看到的,我得到每个条目至少有 2 个评分,但我只想显示每张专辑平均评分最高和最低 2 个评分的歌曲

我试图添加类似的东西

MAX(AVG(vmm_songrating.rating))

在有条款中,但它没有用

我使用 php 和 mysql

感谢您的帮助

编辑 3:mysql 小提琴 http://sqlfiddle.com/#!2/677cf/1/0

只想要前 3 行(专辑中评分最高的歌曲)

4

1 回答 1

0

试试下面的东西..

SELECT MAX(songs.ratingavg), [Other Columns]  
FROM (  
SELECT 
        vmm_user.username, 
        vmm_songs.*, 
        vmm_albums.desc, 
        vmm_albums.release, 
        vmm_albums.name, 
        AVG(vmm_songrating.rating) AS ratingavg,
        COUNT(vmm_songrating.id) AS ratingcount
    FROM 
        vmm_songs 
    LEFT JOIN 
        vmm_user 
    ON 
        vmm_songs.userid=vmm_user.id 
    LEFT JOIN 
        vmm_albums 
    ON 
        vmm_songs.albumid=vmm_albums.id 
    LEFT JOIN  
        vmm_songrating  
    ON 
        vmm_songs.id=vmm_songrating.songid 
    GROUP BY 
        vmm_songs.id
    HAVING 
        COUNT(vmm_songrating.id) >= 2 
    ORDER BY
        AVG(vmm_songrating.rating) DESC
    LIMIT 
        10 ) AS songs
于 2013-07-03T07:12:13.517 回答