This is what I felt when doing the 178. score ranking of the database of leetcode. For details, see: https://blog.csdn.net/weixin_42845682/article/details/105252542
1. The role of dense_rank ()
The role of dense_rank is: to sort. And when there are duplicate values, it does not affect the ranking.
For example, the table is named score and the columns are id and score:
id score
1 80
2 90
3 80
4 40
When using score_rank () to sort scores, the following effects are obtained:
90 1
80 2
80 2
40 3
This method is very useful, but not in mysql ...
Second, how to achieve the effect of dense_rank () in Mysql
as follows:
select
s1.score Score,
(select
count(distinct(s2.score))
from scores s2
where s2.score >= s1.score
) as Rank
from scores s1
order by Rank