获得前5名的总成绩

时间:2015-11-06 19:14:54

标签: mysql sql-order-by

我试图从我们的数据库中获取前10名捐款。每个蒸汽都可以多次捐赠,因此需要多次捐赠才能获得总额。示例表是:

steamid             amount      email                   date
76561197991519598   25          example@example.com     1445107360
76561198129490626   10          example@example.com     1445106920
76561197994977992   5           example@example.com     1445107724
76561197991519598   25          example@example.com     1445107519
76561197994977992   50          example@example.com     1445107047

结果应为:

76561197994977992 = 55
76561197991519598 = 50
76561198129490626 = 10

(从最多到最少排序)。

我自己测试了一些东西并且得到了奇怪的结果,这就是我尝试过的:

SELECT st.*
FROM donations st
WHERE st.amount = 
    (SELECT SUM(t.amount) 
    FROM donations t 
    WHERE t.steamid = st.steamid) 
GROUP BY st.steamid
ORDER BY st.amount

1 个答案:

答案 0 :(得分:1)

试试这个:

select steamid, sum(amount) as total from donations
group by steamid
order by 2 desc
limit 5