Include an additional counter in the MySQL result set

吃可爱长大的小学妹 提交于 2019-11-30 23:37:15

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

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...

Based on the two answers I managed to get the following:

SET @counter = 0; 

Select sub.orderid,sub.value,(@counter := @counter +1) as counter
FROM
(
    select orderid, 
      round(sum(unitprice * quantity),2) as value
    from order_details
    group by orderid
) sub
order by 2 desc
limit 10

The original answers showed the IDs from the inner query resulting in larger ints with huge gaps. Using the modification I get just the '1 to x' range that I need for my pgfplots LaTeX plot.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!