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

MySQL combina select con sum da un'altra tabella

Devi solo raggruppare i tuoi punteggi per utente:

SELECT @p:[email protected]+1 AS position, t.*
FROM (
  SELECT   user.user_id,
           user.user_name,
           IFNULL(SUM(score.score_points),0) AS total_points
  FROM     user LEFT JOIN score ON user.user_id = score.score_user_id
  GROUP BY user.user_id
  ORDER BY total_points DESC
) AS t JOIN (SELECT @p:=0) AS initialisation

Guardalo su sqlfiddle .