Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

SQL JOIN duas tabelas com AVG


Você precisa GROUP BY todos os campos que deseja manter:
SELECT songs.id, songs.song, songs.artist, 
    AVG(score.score * 1.0) AS AvgScore
FROM songs 
    LEFT JOIN score 
        ON score.id=songs.id 
GROUP BY songs.id, songs.song, songs.artist
ORDER BY songs.id, score DESC

Alternativamente, você pode simplesmente fazer isso:
SELECT songs.id, songs.song, songs.artist, 
    (SELECT AVG(Score) FROM score WHERE score.id = songs.id) AS AvgScore)
FROM songs