我目前正在使用此查询来查找玩家的排名:
select coalesce( ( select count(1) from scores b where b.top > a.top OR ( b.top = a.top AND b.time < a.time ) ),0 ) + 1 Rank from Scores a where user = ?
我有一个这样的分数表:
id int user varchar(100) time int (timestamp) top int
最近的一个表是这样的:
id int user varchar(100) time int (timestamp) score int istopscore int (boolean 1/0)
数据库已经填满了数据,所以我不能简单地改变数据库的结构.最近的表中有超过200.000行,因此排序需要花费大量时间.我想尽快找到一种方法来做到这一点.
我如何找到球员的先前排名?这是我尝试过的:
select coalesce( ( select count(1) from recent b where b.istopscore = 1 AND ( ( b.score > a.top AND b.time <= a.time ) OR ( b.score = a.top AND b.time < a.time ) ) ),0) + 1 Rank from scores a where user = ?
此查询的问题在于,如果用户已经对多个新的最高分进行了评分,则会对所有这些评分进行计数,因此它不会给出正确的结果.
任何帮助将不胜感激.
解决方法
我认为您的查询几乎是正确的.要克服多个最高分的问题,您可以使用count(不同的用户名),如
this:
select coalesce( ( select count(distinct username) from recent b where b.istopscore = 1 AND ( ( b.score > a.top AND b.time <= a.time ) OR ( b.score = a.top AND b.time < a.time ) ) ),0) + 1 Rank from scores a where username = 'Echo'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。