我有一个得分表,其中包含my_ql中的user_id得分。每个用户都有很多分数。我想获得每个用户的最佳排名? 我检查了很多解决方案,但所有这些解决方案都适用于每个用户都有单一分数的表格。
我试过这个,但有时用户通过分数得分更高:
SET @rank= 0;;
SELECT Users.username, score, rank
FROM (
SELECT * , @rank := @rank +1 AS rank
FROM Scores
JOIN (
SELECT userid AS user_id2, MAX( score ) AS max_score
FROM Scores
GROUP BY user_id2
ORDER BY max_score DESC
) AS max_score_table ON max_score = Scores.score
AND Scores.userid = user_id2
) derived_table
join Users On (Users.id=derived_table.user_id2)
ORDER BY rank
LIMIT 0, 100