注:MySQL版本8.0以上才支持以下函数
一、ROW_NUMBER()
ROW_NUMBER()函数在排名时序号 连续且不重复,即使表中的两个一样的值时也是这样
原数据:
sql代码:
select *,row_number() OVER(order by number ) as row_num
from num
ORDER BY id
排序后:
二、rank()
Rank() 函数会把要求排序的值相同的归为一组且每组序号一样,排序不会连续执行,即排序时 重复且不连续
sql代码:
select *,rank() OVER(order by number ) as row_num
from num
排序后:
三、dense_rank()
Dense_rank() 函数排序是连续的,也会把相同的值分为一组且每组排序号一样,即排序时 重复且连续
sql代码:
select *,dense_rank() OVER(order by number ) as row_num
from num
排序后:
四、ntile()
Ntile(group_num) 将所有记录分成group_num个组,每组序号一样
sql代码:
select *,ntile(2) OVER(order by number ) as row_num
from num
排序后:
参考链接:https://leetcode.cn/problems/rank-scores/solution/si-da-pai-ming-han-shu-he-guan-jian-zi-b-qvaz/