Mysql
 sql >> Database >  >> RDS >> Mysql

SQL UNISCI due tabelle con AVG

Devi RAGGRUPPARE PER tutti i campi che vuoi conservare:

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

In alternativa, puoi semplicemente fare questo:

SELECT songs.id, songs.song, songs.artist, 
    (SELECT AVG(Score) FROM score WHERE score.id = songs.id) AS AvgScore)
FROM songs