mysql - 按下一个最大数字排序

标签 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。(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

最佳答案

假设您对第 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 racing_id,但返回 Final_results 表中的所有内容。不确定所有这些列都依赖于 racing_id 的单个值(但不能确定,因为我不知道 Final_results 和 racing_trainings 是如何布局的)。

关于mysql - 按下一个最大数字排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405119/

相关文章:

mysql - 向 IN 变量添加值

Mysql:ORDER BY子句中的最大列数?

mysql - 如何按特定顺序连接连接表中的值,mysql

sql - 使用 ORDER BY 时可重复的结果

mysql - 根据匹配数据插入数据

php - 如何从 PHP 检查网络连接或数据库服务器的负载?

c# - 如何将 MySQL.Data 与我的 ClickOnce 应用程序打包在一起?

sql - 根据日期自动在 MySQL 服务器上创建表?

mysql - DateDiff SQL 错误

sql - 如何运行从 pgadmin 的 Postgresql 中的 SELECT FORMAT 构建的查询中获取结果?