在MySQL结果集中包含一个额外的计数器 [英] Include an additional counter in the MySQL result set

查看:70
本文介绍了在MySQL结果集中包含一个额外的计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在MySQL结果集中包含一个额外的计数器吗?我有以下查询,这给了我两列.我需要另外一列(仅在结果中)来指示结果集中每一行的行.

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

推荐答案

尝试一下:

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

这篇关于在MySQL结果集中包含一个额外的计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆