I have a high scores table scores: (id int, deviceid int, value int, board int).
I'd like to display a position for a specific player. At the moment I'm doing this (in PHP):
I select all boards where the player has a score and the score itself:
select board, min(value) as value
from scores
where deviceid = 1234
group by board
Then for each row ($board,$value) I select:
select count(id) from scores
where board = $board and value < $value
by selecting number of rows for specific board with score less than specified value, I get the player's position (first player would get position 0, so it will be increased by 1 when displayed)
I know this is horribly unefficient, so I'm looking for a way to do it faster. It could possibly be done in a stored procedure with a cursor, but for 100 boards, I will need to do at most 1+100 selects.
I a nutshell, I would like to do something like this, in pseudo-sql:
select board, min(value) as val,
count(id) from _this_group_ where value < val
from scores
where deviceid = 1234
group by board