php – 在这个得分表中找到我以前的排名会有什么好的查询?
发布时间:2020-12-13 16:00:22 所属栏目:PHP教程 来源:网络整理
导读:我目前正在使用此查询来查找玩家的排名: 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 Rankfrom Scores awhere user = ? 我有一个这样的分数表: id intuser varchar(100)time i
我目前正在使用此查询来查找玩家的排名:
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' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |