获得前 5 个总结果

Get top 5 total results

我试图从我们的数据库中获取前 10 名捐款。每个 steamid 可以捐赠多次,因此需要将多次捐赠加在一起以获得总数。一个例子 table 是:

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

试试这个:

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