MySQL - Get row number on select

后端 未结 5 2133
轻奢々
轻奢々 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 01:12

    SELECT @rn:=@rn+1 AS rank, itemID, ordercount
    FROM (
      SELECT itemID, COUNT(*) AS ordercount
      FROM orders
      GROUP BY itemID
      ORDER BY ordercount DESC
    ) t1, (SELECT @rn:=0) t2;
    

提交回复
热议问题