如果我们有表,排行榜最有效的 SQL 查询是什么:
[UserID], [Points]
我希望结果按积分和排名列排序。
最佳答案
在 SQL Server 2008 中,您可以使用 Ranking functions :
SELECT UserId,
Points,
RANK() OVER(ORDER BY Points DESC) AS Rank
FROM LeaderBoards
ORDER BY Points DESC
A MySql rank could be something like this:
SELECT UserId,
Points,
@curRank := @curRank + 1 AS Rank
FROM LeaderBoards l, (SELECT @curRank := 0) r
ORDER BY Points DESC
关于mysql - 用于排序排行榜的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105080/