我有一张高分表scores: (id int, deviceid int, value int, board int)
。我想显示特定玩家的位置。目前我正在这样做(在 PHP 中):
我选择玩家有分数的所有棋盘和分数本身:
select board, min(value) as value
from scores
where deviceid = 1234
group by board
然后对于每一行 ($board,$value) 我选择:
select count(id) from scores
where board = $board and value < $value
通过选择分数小于指定值的特定板的行数,我得到玩家的位置(第一个玩家将获得位置 0,因此显示时它将增加 1)
我知道这是非常低效的,所以我正在寻找一种更快的方法。它可以在带有游标的存储过程中完成,但是对于 100 个板,我最多需要进行 1+100 次选择。
简而言之,我想在伪 sql 中做这样的事情:
select board, min(value) as val,
count(id) from _this_group_ where value < val
from scores
where deviceid = 1234
group by board