mysql-rank排名问题(添加行号)

create table score (id int primary key, score int , orders int );
insert into score values(5,20);
SET @prev_value = NULL;
SET @rank_count = 0;
SELECT id, score, CASE
WHEN @prev_value = score THEN @rank_count
WHEN @prev_value := score THEN @rank_count := @rank_count + 1
END AS rank
FROM score
ORDER BY score [desc];

-----------------------------------------
--实例

[mysql - root@[email protected] 14:10:42] >create table score (id int primary key, score int );
Query OK, 0 rows affected (0.01 sec)

[mysql - root@[email protected] 14:10:46] >show create table score\G;
*************************** 1. row ***************************
Table: score
Create Table: CREATE TABLE `score` (
`id` int(11) NOT NULL,
`score` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

ERROR:
No query specified

[mysql - root@[email protected] 14:10:51] >insert into score values(1,20);
Query OK, 1 row affected (0.01 sec)

[mysql - root@[email protected] 14:11:10] >insert into score values(2,30);
Query OK, 1 row affected (0.00 sec)

[mysql - root@[email protected] 14:11:14] >insert into score values(3,30);
Query OK, 1 row affected (0.00 sec)

[mysql - root@[email protected] 14:11:18] >insert into score values(4,40);
Query OK, 1 row affected (0.01 sec)

[mysql - root@[email protected] 14:11:22] >insert into score values(5,10);
Query OK, 1 row affected (0.01 sec)

[mysql - root@[email protected] 14:11:26] >insert into score values(6,90);
Query OK, 1 row affected (0.00 sec)

[mysql - root@[email protected] 14:11:37] >insert into score values(7,20);
Query OK, 1 row affected (0.01 sec)

[mysql - root@[email protected] 14:13:05] >SET @prev_value = NULL;
Query OK, 0 rows affected (0.00 sec)

[mysql - root@[email protected] 14:13:23] >SET @rank_count = 0;
Query OK, 0 rows affected (0.00 sec)

[mysql - root@[email protected] 14:13:23] >SELECT id, score, CASE
-> WHEN @prev_value = score THEN @rank_count
-> WHEN @prev_value := score THEN @rank_count := @rank_count + 1
-> END AS rank
-> FROM score
-> ORDER BY score;
+----+-------+------+
| id | score | rank |
+----+-------+------+
| 5 | 10 | 1 |
| 1 | 20 | 2 |
| 7 | 20 | 2 |
| 2 | 30 | 3 |
| 3 | 30 | 3 |
| 4 | 40 | 4 |
| 6 | 90 | 5 |
+----+-------+------+
7 rows in set (0.00 sec)

[mysql - root@[email protected] 14:13:23] >SET @prev_value = NULL;
Query OK, 0 rows affected (0.00 sec)

[mysql - root@[email protected] 14:13:35] >SET @rank_count = 0;
Query OK, 0 rows affected (0.00 sec)

[mysql - root@[email protected] 14:13:35] >SELECT id, score, CASE
-> WHEN @prev_value = score THEN @rank_count
-> WHEN @prev_value := score THEN @rank_count := @rank_count + 1
-> END AS rank
-> FROM score
-> ORDER BY score desc;
+----+-------+------+
| id | score | rank |
+----+-------+------+
| 6 | 90 | 1 |
| 4 | 40 | 2 |
| 2 | 30 | 3 |
| 3 | 30 | 3 |
| 1 | 20 | 4 |
| 7 | 20 | 4 |
| 5 | 10 | 5 |
+----+-------+------+
7 rows in set (0.00 sec)

[mysql - root@[email protected] 14:13:36] >

猜你喜欢

转载自www.cnblogs.com/smallfishy/p/12916944.html