我正在尝试获取每个用户的所有总积分,以降序对其进行排序并打印其排名。我的查询工作正常,但它不能正确设置用户的排名。
SET @rank := 0;
SELECT (@rank := @rank + 1) AS rank, u.username, SUM(s.score) AS totalScore
FROM solution AS s
INNER JOIN users u ON u.id = s.author_id
GROUP BY u.username
ORDER BY totalScore DESC
用户架构:
|---------------------|------------------|
| id | username |
|---------------------|------------------|
| 1 | test1 |
|---------------------|------------------|
| 2 | test2 |
|---------------------|------------------|
| 3 | test3 |
|---------------------|------------------|
| 8 | test4 |
|---------------------|------------------|
解决方案架构:
|---------------------|------------------|-----------------|
| id | author_id | score |
|---------------------|------------------|-----------------|
| 1 | 1 | 55 |
|---------------------|------------------|-----------------|
| 2 | 2 | 5 |
|---------------------|------------------|-----------------|
| 3 | 3 | 22 |
|---------------------|------------------|-----------------|
| 4 | 8 | 43 |
|---------------------|------------------|-----------------|
| 5 | 8 | 43 |
|---------------------|------------------|-----------------|
结果是:
|---------------------|------------------|-----------------|
| rank | username | totalScore |
|---------------------|------------------|-----------------|
| 4 | test4 | 86 |
|---------------------|------------------|-----------------|
| 1 | test1 | 55 |
|---------------------|------------------|-----------------|
| 3 | test3 | 22 |
|---------------------|------------------|-----------------|
| 2 | test2 | 5 |
|---------------------|------------------|-----------------|
为什么会这样?
预期结果应该是:
|---------------------|------------------|-----------------|
| rank | username | totalScore |
|---------------------|------------------|-----------------|
| 1 | test4 | 86 |
|---------------------|------------------|-----------------|
| 2 | test1 | 55 |
|---------------------|------------------|-----------------|
| 3 | test3 | 22 |
|---------------------|------------------|-----------------|
| 4 | test2 | 5 |
|---------------------|------------------|-----------------|
这将从MySql运行:
Select username, totalScore, Rank() over (order by totalScore desc) as Rank
from (
SELECT u.username, SUM(s.score) AS totalScore
FROM solution AS s
INNER JOIN users u ON u.id = s.author_id
GROUP BY u.username
ORDER BY totalScore DESC
)t
正如我在评论中所说。您首先要对分数进行排名才能对其进行排名。
SELECT
(@rank:=@rank + 1) rank, username, totalscore
FROM
(SELECT
u.username, SUM(s.score) AS totalScore
FROM
solution AS s
INNER JOIN users u ON u.id = s.author_id
GROUP BY u.username
ORDER BY totalScore DESC) t1,
(SELECT @rank:=0) r1
请参见示例,第二个查询是我的
模式(MySQL v5.7)
CREATE TABLE users (
`id` INTEGER,
`username` VARCHAR(5)
);
INSERT INTO users
(`id`, `username`)
VALUES
('1', 'test1'),
('2', 'test2'),
('3', 'test3'),
('8', 'test4');
CREATE TABLE solution (
`id` INTEGER,
`author_id` INTEGER,
`score` INTEGER
);
INSERT INTO solution
(`id`, `author_id`, `score`)
VALUES
('1', '1', '55'),
('2', '2', '5'),
('3', '3', '22'),
('4', '8', '43'),
('5', '8', '43');
查询#1
SET @rank := 0;
没有要显示的结果。
查询#2
SELECT (@rank := @rank + 1) AS rank, u.username, SUM(s.score) AS totalScore
FROM solution AS s
INNER JOIN users u ON u.id = s.author_id
GROUP BY u.username
ORDER BY totalScore DESC;
| rank | totalScore | username |
| ---- | ---------- | -------- |
| 4 | 86 | test4 |
| 1 | 55 | test1 |
| 3 | 22 | test3 |
| 2 | 5 | test2 |
查询#3
SELECT
(@rank := @rank +1) rank
,username
,totalscore
FROM
(SELECT
u.username,
SUM(s.score) AS totalScore
FROM solution AS s
INNER JOIN users u ON u.id = s.author_id
GROUP BY u.username
ORDER BY totalScore DESC) t1,(SELECT @rank := 0) r1;
| rank | username | totalscore |
| ---- | -------- | ---------- |
| 1 | test4 | 86 |
| 2 | test1 | 55 |
| 3 | test3 | 22 |
| 4 | test2 | 5 |