CREATE TABLE `tmp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sal` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
+----+-----+
| id | sal |
+----+-----+
| 1 | 100 |
| 2 | 300 |
| 3 | 200 |
| 4 | 200 |
+----+-----+
没有并列时1234,有并列时1224
mysql> select t1.id,(select t3.sal from tmp t3 where t1.id=t3.id) sal,count(t2.sal)+1 `rank` from tmp t1 left join tmp t2 on t1.sal<t2.sal group by t1.id order by `rank`;
+----+-----+------+
| id | sal | rank |
+----+-----+------+
| 2 | 300 | 1 |
| 3 | 200 | 2 |
| 4 | 200 | 2 |
| 1 | 100 | 4 |
+----+-----+------+
4 rows in set (0.06 sec)
mysql>
select *,rank() over (order by sal) as 'rank' from sal;
+----+-----+------+
| id | sal | rank |
+----+-----+------+
| 2 | 300 | 1 |
| 3 | 200 | 2 |
| 4 | 200 | 2 |
| 1 | 100 | 4 |
+----+-----+------+