所以我想在 mysql 中创建一个 toplist。 我有两个表,用户和团队。
在用户中我有:id,name,points,teamid
在团队中我有:t_id,teamname,leaderid
我想列一个排行榜。得分最高的 10 支队伍。
我希望我想要的是可以理解的。
最佳答案
您需要通过使用子查询来计算总分总和,该子查询将为您提供前 10 分,然后将此结果与包含团队信息和每个团队总分的查询结合起来,如果有2支队伍打平将进入积分前10名
select t2.* from
(select t.*,sum(u.points) allpoints
from teams t
join users u
on(t.t_id= u.teamid)
group by t.t_id
) t2
join
(select sum(points) allpoints
from users
group by teamid
order by allpoints desc
limit 10) tp
on(t2.allpoints = tp.allpoints )
如果你不关心领带场景,你可以简单地使用限制和排序
select t.*,sum(u.points) allpoints
from teams t
join users u
on(t.t_id= u.teamid)
group by t.t_id
order by allpoints desc
limit 10
关于MYSQL Toplist 按团队成员积分总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321093/