Rank() over Partition by in mysql

自作多情 提交于 2019-11-27 09:39:23

Try this query: - MySql does not support Rank() function.

select result.id,result.login,result.rank from (
  SELECT    id,
              login,
              IF(login=@last,@curRank:=@curRank,@curRank:=@_sequence) AS rank,
              @_sequence:=@_sequence+1,
              @last:=login
    FROM      ds , (SELECT @curRank := 1, @_sequence:=1, @last:=0) r
    ORDER BY  id asc) as result;

Hope it helps you!

After Mysql 8.0 you can use Rank function

 RANK() OVER (
    ORDER BY column_name
) my_rank

RANK() OVER (
PARTITION BY <expression>[{,<expression>...}]
ORDER BY <expression> [ASC|DESC], [{,<expression>...}]
)

here is the usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!