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@localhost@testmysql.sock 14:10:42] >create table score (id int primary key, score int );
Query OK, 0 rows affected (0.01 sec)
[mysql - root@localhost@testmysql.sock 14:10:46] >show create table score\G;
*************************** 1. row ***************************
Table: score
Create Table: CREATE TABLE `score` (
`id` int(11) NOT NUL