近日遇到一个SQL问题:将一个表按照分数排序,分数相同的名次相同。
如下表:Scores
Id Score
1 3
2 3
3 4
4 4
5 5
要求查询出的结果如下所示:
Score Rank
5 1
4 2
4 2
3 3
3 3
思考了一会儿我的思路是:
用Scores表连接Scores表,看在另一个表中在多少个唯一的分数大于扫描到的分数。
SQL:
SELECT sc.score,(select count(distinct s.score) from scores s where s.score>=sc.score) as rank from scores sc order by rank;
记录在这儿,以便以后回顾。