Include an additional counter in the MySQL result set
Solution 1
Try this:
SET @counter = 0;
Select sub.*
FROM
(
select orderid, (@counter := @counter +1) as counter,
round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
) sub
order by 2 desc
Solution 2
Try following
SET @counter = 0;
select orderid, (@counter:= @counter + 1) as counter, round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
order by 3 desc
limit 10
Hope it helps...
Uwe Ziegenhagen
I have been working with LaTeX since more than two decades, with focus on the the automation of the typesetting process. Besides LaTeX I am a huge fan of Python, work a lot with pandas. Some of my German & English articles can be found here: http://uweziegenhagen.de/?page_id=159 I am member and board member of Dante e.V. (http://www.dante.de), the German-speaking TeX/LaTeX users group and contribute regularly to Dante e.V.'s magazine, the "Die TeXnische Komödie". If you want to contact me, use @gmail.com
Updated on June 07, 2022Comments
-
Uwe Ziegenhagen about 2 years
Can I include an additional counter in a MySQL result set? I have the following query which gives me two columns back. I need an additional column (only in the result) indicating the row of each line in the result set.
select orderid, round(sum(unitprice * quantity),2) as value from order_details group by orderid order by 2 desc limit 10
I need something like the following:
10865 1 17250.00 11030 2 16321.90 10981 3 15810.00 10372 4 12281.20 10424 5 11493.20