How to get a row rank?

后端 未结 6 1955
一生所求
一生所求 2021-01-05 04:08

HI,

I actually posted similar (or same?) question yesterday, but I thought I need to post a new question since I have short, but clear question.

I have the f

6条回答
  •  温柔的废话
    2021-01-05 04:33

    In mysql 8 you can use window function like:

    SELECT
      id,
      score,
      rank() over (order by amount) as ranking
    FROM
      SomeTable
    

    If you need to select only one row, use a subquery:

    SELECT
      id
      score,
      ranking
    FROM (
      SELECT
        id,
        score,
        rank() over (order by score) as ranking
      FROM
        SomeTable
    ) t
    WHERE
      id = ?
    

提交回复
热议问题