php 查某记录 的排行,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 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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值