gpt4 book ai didi

php - 在此分数表中查找我以前的排名的查询是什么?

转载 作者:可可西里 更新时间:2023-11-01 07:39:00 25 4
gpt4 key购买 nike

我目前正在使用此查询来查找玩家的排名:

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(distinct username),例如 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'

关于php - 在此分数表中查找我以前的排名的查询是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com