$sql = "set @rank :=0, @pos :=0, @game := NULL, @score := NULL; UPDATE `af_score_ranked` SET `rank_in_game` = greatest( @rank := if( @score = `score`, @rank , @rank +1 ) least( 0, @score := `score` ) ) ORDER BY `score` DESC" or die(myMysqlError($sql, mysql_error(), __LINE__, __FILE__));