MySQL - Get row number on select

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

    You can use MySQL variables to do it. Something like this should work (though, it consists of two queries).

    SELECT 0 INTO @x;
    
    SELECT itemID, 
           COUNT(*) AS ordercount, 
           (@x:=@x+1) AS rownumber 
    FROM orders 
    GROUP BY itemID 
    ORDER BY ordercount DESC; 
    

提交回复
热议问题