MySQL视窗函数row_number(), rank(), denser_rank()

原表:

MySQL视窗函数row_number(), rank(), denser_rank()

SELECT 
    *,
    row_number() over(PARTITION BY department ORDER BY cost ASC) AS row_number_,
    rank() over(PARTITION BY department ORDER BY cost ASC) AS rank_,
    dense_rank() over(PARTITION BY department ORDER BY cost ASC) AS dense_rank_
FROM  table_1;

结果如下:

MySQL视窗函数row_number(), rank(), denser_rank()

PARTITION BY:要分类的字段,这里用department里面的A和B分类
ORDER BY:要按“XX”字段排序,这里用的是cost排序
其他的部分都是固定格式,可以看到:
row_number():即使cost的值相同,它也按照12345这样排序

rank():遇到相同的cost值的时候,它的排序是113(A), 1114(B)

dense_rank():它会把相同的值仅仅记为一个,112(A), 11122(B)

tips:括号里面是department名



 

MySQL视窗函数row_number(), rank(), denser_rank()

上一篇:SQL-mysql锁等待与死锁


下一篇:MySQL的事务与锁 转