MySQL - Get row number on select

后端 未结 5 2278
轻奢々
轻奢々 2020-11-22 00:43

Can I run a select statement and get the row number if the items are sorted?

I have a table like this:

mysql> describe orders;
+-------------+----         


        
5条回答
  •  暖寄归人
    2020-11-22 00:55

    It's now builtin in MySQL 8.0 and MariaDB 10.2:

    SELECT
      itemID, COUNT(*) as ordercount,
      ROW_NUMBER OVER (PARTITION BY itemID ORDER BY rank DESC) as rank
    FROM orders
    GROUP BY itemID ORDER BY rank DESC
    

提交回复
热议问题