且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL将select与其他表中的sum结合起来

更新时间:2023-12-01 14:09:04

你只需要将你的分数按用户分组:

You just need to group your scores by user:

SELECT @p:=@p+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

sqlfiddle 上查看它.