使用 mysql 根据用户的最高得分计算用户排名
Calculate rank of the users based on their max score using mysql
我有一个 table(称为用户)我需要根据他们的分数对用户进行排名,但我希望根据用户的最高分数进行排名。
+-----------+------------+
| User_id | Score |
+-----------+------------+
| 1 | 12258 |
| 1 | 112 |
| 2 | 9678 |
| 5 | 9678 |
| 3 | 689206 |
| 3 | 1868 |
期待结果
+-----------+------------+---------+
| User_id | Score | Rank |
+-----------+------------+---------+
| 3 | 689206 | 1 |
| 1 | 12258 | 2 |
| 2 | 9678 | 3 |
| 5 | 9678 | 3 |
您正在寻找DENSE_RANK
,但它支持mysql高于8.0
的版本
- 使用相关子查询通过每个
User_id
获取 max
值
- 使用两个变量,一个用于存储
rank
另一个用于存储先前的值以生成 DENSE_RANK
数字。
看起来像这样。
CREATE TABLE T(
User_id int,
Score int
);
insert into t values (1,12258);
insert into t values (1,112);
insert into t values (2,9678);
insert into t values (5,9678);
insert into t values (3,689206);
insert into t values (3,1868);
查询 1:
SELECT User_id,Score,Rank
FROM (
SELECT User_id,
Score,
@rank :=IF(@previous = t1.score, @rank, @rank + 1) Rank,
@previous := t1.Score
FROM T t1 CROSS JOIN (SELECT @Rank := 0,@previous := 0) r
WHERE t1.Score =
(
SELECT MAX(Score)
FROM T tt
WHERE t1.User_id = tt.User_id
)
ORDER BY Score desc
) t1
| User_id | Score | Rank |
|---------|--------|------|
| 3 | 689206 | 1 |
| 1 | 12258 | 2 |
| 2 | 9678 | 3 |
| 5 | 9678 | 3 |
MySql 5.7 中计算 DENSE_RANK 的另一个技巧(与 MySql 8 中的一样)是使用带有变量赋值的 CASE WHEN。
SELECT User_id, MaxScore AS Score,
CASE
WHEN MaxScore = @prevScore THEN @rnk
WHEN @prevScore := MaxScore THEN @rnk := @rnk+1
ELSE @rnk := @rnk+1
END AS Rank
FROM
(
SELECT User_id, MAX(Score) AS MaxScore
FROM YourTable
GROUP BY User_id
ORDER BY MaxScore DESC, User_id
) AS q
CROSS JOIN (SELECT @rnk := 0, @prevScore := null) AS vars
您可以在 rextester 上测试 here。
我有一个 table(称为用户)我需要根据他们的分数对用户进行排名,但我希望根据用户的最高分数进行排名。
+-----------+------------+
| User_id | Score |
+-----------+------------+
| 1 | 12258 |
| 1 | 112 |
| 2 | 9678 |
| 5 | 9678 |
| 3 | 689206 |
| 3 | 1868 |
期待结果
+-----------+------------+---------+
| User_id | Score | Rank |
+-----------+------------+---------+
| 3 | 689206 | 1 |
| 1 | 12258 | 2 |
| 2 | 9678 | 3 |
| 5 | 9678 | 3 |
您正在寻找DENSE_RANK
,但它支持mysql高于8.0
- 使用相关子查询通过每个
User_id
获取 - 使用两个变量,一个用于存储
rank
另一个用于存储先前的值以生成DENSE_RANK
数字。
max
值
看起来像这样。
CREATE TABLE T(
User_id int,
Score int
);
insert into t values (1,12258);
insert into t values (1,112);
insert into t values (2,9678);
insert into t values (5,9678);
insert into t values (3,689206);
insert into t values (3,1868);
查询 1:
SELECT User_id,Score,Rank
FROM (
SELECT User_id,
Score,
@rank :=IF(@previous = t1.score, @rank, @rank + 1) Rank,
@previous := t1.Score
FROM T t1 CROSS JOIN (SELECT @Rank := 0,@previous := 0) r
WHERE t1.Score =
(
SELECT MAX(Score)
FROM T tt
WHERE t1.User_id = tt.User_id
)
ORDER BY Score desc
) t1
| User_id | Score | Rank |
|---------|--------|------|
| 3 | 689206 | 1 |
| 1 | 12258 | 2 |
| 2 | 9678 | 3 |
| 5 | 9678 | 3 |
MySql 5.7 中计算 DENSE_RANK 的另一个技巧(与 MySql 8 中的一样)是使用带有变量赋值的 CASE WHEN。
SELECT User_id, MaxScore AS Score,
CASE
WHEN MaxScore = @prevScore THEN @rnk
WHEN @prevScore := MaxScore THEN @rnk := @rnk+1
ELSE @rnk := @rnk+1
END AS Rank
FROM
(
SELECT User_id, MAX(Score) AS MaxScore
FROM YourTable
GROUP BY User_id
ORDER BY MaxScore DESC, User_id
) AS q
CROSS JOIN (SELECT @rnk := 0, @prevScore := null) AS vars
您可以在 rextester 上测试 here。