我想根据总分列对我的参赛作品进行排名,但如果分数相同,我需要有联系。下面的查询成功排名,但即使有平局,排名也会上升。
这是我当前的查询:
SET @r=0; UPDATE table SET rank= @r:= (@r+1) WHERE `complete` = 1 ORDER BY score DESC;
我一直在阅读大量帖子,但没有在更新中找到解释。
+----+-----------+-------------+-------------+
| id | lName | score | rank |
+----+-----------+-------------+-------------+
| 1 | Rosebrock | 50 | 5 |
| 2 | Kolley | 58 | 3 |
| 3 | Bartley | 11 | 6 |
| 4 | Bell | 58 | 4 |
| 5 | Daugherty | 63 | 2 |
| 85 | Harp | 78 | 1 |
+----+-----------+-------------+-------------+
答案 0 :(得分:3)
create table scores (
id INT NOT NULL AUTO_INCREMENT,
score INT NOT NULL,
rank INT NOT NULL DEFAULT 0,
PRIMARY KEY (id)
) ENGINE=InnoDB;
INSERT INTO scores (score) VALUES (1), (20), (10), (25), (10), (20);
SET @rank=0, @score=-100;
UPDATE scores
SET rank=IF(@score=(@score:=score), @rank, @rank:=@rank+1)
ORDER BY score;
答案 1 :(得分:1)
有关在MySQL中实施密集排名的一些想法,请参阅此问题/答案: