两个表我试图连接两个表: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