MySQL equivalent of ORACLES rank()

前端 未结 2 1528
一向
一向 2020-12-01 20:19

Oracle has 2 functions - rank() and dense_rank() - which i\'ve found very useful for some applications. I am doing something in mysql now and was wondering if they have some

2条回答
  •  自闭症患者
    2020-12-01 20:47

    Nothing directly equivalent, but you can fake it with some (not terribly efficient) self-joins. Some sample code from a collection of MySQL query howtos:

    SELECT v1.name, v1.votes, COUNT(v2.votes) AS Rank
    FROM votes v1
    JOIN votes v2 ON v1.votes < v2.votes OR (v1.votes=v2.votes and v1.name = v2.name)
    GROUP BY v1.name, v1.votes
    ORDER BY v1.votes DESC, v1.name DESC;
    +-------+-------+------+
    | name  | votes | Rank |
    +-------+-------+------+
    | Green |    50 |    1 |
    | Black |    40 |    2 |
    | White |    20 |    3 |
    | Brown |    20 |    3 |
    | Jones |    15 |    5 |
    | Smith |    10 |    6 |
    +-------+-------+------+ 
    

提交回复
热议问题