MySQL - Get row number on select

后端 未结 5 2152
轻奢々
轻奢々 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:52

    Take a look at this.

    Change your query to:

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

    The last select is your count.

提交回复
热议问题