Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created December 23, 2018 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/3fac885274917ad6c96a96f148e6df05 to your computer and use it in GitHub Desktop.
Save Kcko/3fac885274917ad6c96a96f148e6df05 to your computer and use it in GitHub Desktop.
id game_id game_rank score team_id
-------------------------------------------
5 1 1 15 1
4 1 2 25 2
1 1 3 40 3
3 1 3 40 4
2 1 4 55 5
7 2 0 0 1
6 2 0 0 2
#1
SET @lastscore = 0;
SET @ordering = 0;
UPDATE game_scores
SET
game_rank = IF(score = @lastscore, @lastscore, (@ordering := @ordering + 1))
, score = (@lastscore := score)
WHERE game_id = 1
ORDER BY score;
#2
UPDATE game_scores
CROSS JOIN ( SELECT @lastscore:=0, @ordering:=0) AS parameter
SET
game_rank = IF(score = @lastscore, @lastscore, (@ordering := @ordering + 1))
, score = (@lastscore := score)
WHERE game_id = 1
ORDER BY score;
#3
SET @lastscore := 0;
SET @ordering := 0;
UPDATE game_scores SET
IF(@lastscore = score, @ordering, @ordering := @ordering + 1),
game_rank = @ordering,
@lastscore := score
WHERE game_id = 1
ORDER BY score;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment