按支出金额选择排名前10位的用户

时间:2018-08-23 05:09:19

标签: mysql

所以我有一个如下表:

|     steamid | payment_amount |
       199  5.00
       501  10.00
       199  15.00
       238  5.00
       238  1.00

所需的输出:

 199 20.00
 501 10.00
 238 6.00

我试图选择按Steamid与Payment_amount的总和分组的数据,但是没有按预期出来?

例如:

SELECT steamid, sum(payment_amount) AS total FROM payments GROUP BY steamid

2 个答案:

答案 0 :(得分:0)

SELECT steamid, SUM(payment_amount) AS total FROM payments GROUP BY steamid ORDER BY total DESC

通过按上述查询中的总desc顺序添加顺序,您可以在查询中进行较小的更改即可获得结果。

答案 1 :(得分:0)

  • 添加ORDER BY total DESC以按降序对结果进行排序

  • 添加LIMIT 10以选择前10名

结果声明:

SELECT steamid, sum(payment_amount) AS total FROM payments GROUP BY steamid ORDER BY total DESC LIMIT 10