我是sql的新手,我从来没有在mysql中使用变量或条件,但是从其他编程语言中知道.几天后,我试图找到一种方法来对用户评分进行排名.我阅读了很多文章,还有关于stackoverflow的问题,最后我找到了一个几乎像我想要的解决方案.
SELECT
score_users.uid,
score_users.score,
@prev := @curr,
@curr := score,
@rank := IF(@prev = @curr, @rank, @rank +1) AS rank
FROM
score_users,
(SELECT @curr := null, @prev := null, @rank := 0) tmp_tbl
WHERE
score_users.matchday = 1
ORDER BY
score_users.score DESC
Run Code Online (Sandbox Code Playgroud)
但我的问题是平局得分.我不希望得到连续的排名,像这样:
+------------+------+--------+
| uid | name | rank | score |
+------------+------+--------+
| 4 | Jon | 1 | 20 |
| 1 | Jane | 2 | 19 |
| 2 | Fred | 2 | 19 | …Run Code Online (Sandbox Code Playgroud)