按下一个最高编号排序

时间:2014-06-25 09:35:54

标签: mysql sql sql-order-by

我的查询以几个练习为主角,对游戏玩家的排名进行了调整。如果他们的得分相关,我需要在每次练习中以最高排名解开。

例如:

     total_points pos1 pos2 pos3
#1   5            1    3    1
#2   7            3    1    3
#3   7            4    1T   2

第二名将是目前排名第3的球员,因为他在第二名的位置上排名第一,但是他排在第二位,而第二位是第二名,排在第二位的是第二名。下一个更高的3.(T表示并列,当玩家与另一个玩家并列时,应该出现在数字旁边)

所以期望的结果是

  total_points pos1 pos2 pos3
#1   5            1    3    1
#2   7            4    1T   2
#3   7            3    1    3

我认为排序是通过pos1排序然后忽略其他位置?

我使用的后续查询是

 SELECT totals.*, scores.*, warriors.*, results.*, positions.* 
       FROM (SELECT results . * , SUM( points ) AS total_points 
           FROM final_results AS results 
           INNER JOIN contest_trainings AS wods ON wods.id = results.wod_id 
           WHERE wods.show_date < NOW( ) 
           GROUP BY contest_id) AS totals, 
       final_results AS scores 
       INNER JOIN (
           SELECT wod1.*, wod1.wod_position AS pos1, wod2.wod_position AS pos2, wod3.wod_position AS pos3 
           FROM final_results AS wod1 
           LEFT OUTER JOIN final_results AS wod2 
           ON wod1.contest_id = wod2.contest_id 
           AND wod2.wod_id > wod1.wod_id 
           LEFT OUTER JOIN final_results AS wod3 
           ON wod2.contest_id = wod3.contest_id 
           AND wod3.wod_id > wod2.wod_id 
           GROUP BY wod1.contest_id) as positions 
        ON positions.contest_id = scores.contest_id 
        INNER JOIN contest AS warriors ON scores.contest_id = warriors.id 
        INNER JOIN contest_results AS results ON scores.contest_id = results.contest_id 
        WHERE totals.contest_id = warriors.id AND results.validated = 1 
        AND warriors.battle = 1 
        AND warriors.category = 1 
        GROUP BY scores.contest_id 
        ORDER BY totals.total_points, 
        positions.pos1, CAST(positions.pos2 AS UNSIGNED), positions.pos3

1 个答案:

答案 0 :(得分:0)

假设您对第4个位置感兴趣,那么我会尝试使用以下类型: -

SELECT totals.*, scores.*, warriors.*, results.*, positions.* 
FROM 
(
    SELECT results . * , SUM( points ) AS total_points 
    FROM final_results AS results 
    INNER JOIN contest_trainings AS wods 
    ON wods.id = results.wod_id 
    WHERE wods.show_date < NOW( ) 
    GROUP BY contest_id
) AS totals, 
CROSS JOIN final_results AS scores 
INNER JOIN 
(
    SELECT wod1.*, wod1.wod_position AS pos1, wod2.wod_position AS pos2, wod3.wod_position AS pos3 
    FROM final_results AS wod1 
    LEFT OUTER JOIN final_results AS wod2 
    ON wod1.contest_id = wod2.contest_id 
    AND wod2.wod_id > wod1.wod_id 
    LEFT OUTER JOIN final_results AS wod3 
    ON wod2.contest_id = wod3.contest_id 
    AND wod3.wod_id > wod2.wod_id 
    GROUP BY wod1.contest_id
) as positions 
ON positions.contest_id = scores.contest_id 
INNER JOIN contest AS warriors ON scores.contest_id = warriors.id 
INNER JOIN contest_results AS results ON scores.contest_id = results.contest_id 
WHERE totals.contest_id = warriors.id AND results.validated = 1 
AND warriors.battle = 1 
AND warriors.category = 1 
GROUP BY scores.contest_id 
ORDER BY totals.total_points, 
        IF(positions.pos1 = 1, 1, 0) + IF(positions.pos2 = 1, 1, 0) + IF(positions.pos3 = 1, 1, 0),
        IF(positions.pos1 = 2, 1, 0) + IF(positions.pos2 = 2, 1, 0) + IF(positions.pos3 = 2, 1, 0),
        IF(positions.pos1 = 3, 1, 0) + IF(positions.pos2 = 3, 1, 0) + IF(positions.pos3 = 3, 1, 0),
        IF(positions.pos1 = 4, 1, 0) + IF(positions.pos2 = 4, 1, 0) + IF(positions.pos3 = 4, 1, 0)

有可能改善这一点,但我需要看到更多原始表格。

请注意,您的查询似乎可能会有一些不确定的结果。例如,您的第一个子查询执行 GROUP BY contest_id ,但会返回final_results表中的所有内容。不确定所有这些列是否依赖于contest_id的单个值(但不能确定,因为我不知道final_results和contest_trainings是如何布局的。)