兩個表我試圖連接兩個表:SQL JOIN與AVG
songs
id | song | artist
---|------|-------
1 | foo | bar
2 | fuu | bor
3 | fyy | bir
score
id | score
---|------
1 | 2
2 | 4
3 | 8
2 | 6
3 | 2
使用此SQL命令:
SELECT songs.id, songs.song, songs.artist, score.score FROM songs LEFT JOIN score ON score.id=songs.id ORDER BY songs.id, score DESC
我得到的回覆是有多個得分同一首歌的副本,我希望得分是平均的。
result
id | song | artist | score
---|------|--------|-------
1 | foo | bar | 2
2 | fuu | bor | 4
2 | fuu | bor | 6
3 | fyy | bir | 8
3 | fyy | bir | 2
我試過用:
SELECT songs.id, songs.song, songs.artist, ROUND(AVG(score.score),1) AS 'score' FROM songs INNER JOIN score ON score.id=songs.id ORDER BY score DESC
但是,平均所有得分,而不僅僅是得分每一個人首歌曲的
result
id | song | artist | score
---|------|--------|-------
1 | foo | bar | 4.4
添加'集團By' .. –
成績沒有主鍵,這可能證明是有問題。 – Strawberry