2

我正在尝试创建一个 mysql 查询,根据他们的分数对 MMA 战士进行排名,以及他们是否专业,以及他们的体重等级,共同创建一个战士部门。我有一张包含所有拳手的表格,其中包含他们的体重、状态(专业或业余)和一堆其他列。我还有一个表格,其中包含从他们所有的战斗中计算出来的分数。记录需要按部门和分数排序,但我只能让我的查询按部门排序:

INSERT INTO fa_rankings (fighter_id, rank, division)
SELECT 
    fa_scores.fighter_id,
    @r := CASE WHEN @div <> f_division THEN 1 ELSE @r+1 END as f_rank,
    @div := f_division
FROM
    (SELECT @div := NULL) d,
    (SELECT @r := 0) r,
    fa_scores
JOIN (SELECT fa_fighters.fighter_id,
        CONCAT(
            IF(status = 'professional', 'a', 'b'),
            CASE
                WHEN weight < 126 THEN 0
                WHEN weight BETWEEN 126 AND 135 THEN 1
                WHEN weight BETWEEN 136 AND 145 THEN 2
                WHEN weight BETWEEN 146 AND 155 THEN 3
                WHEN weight BETWEEN 156 AND 170 THEN 4
                WHEN weight BETWEEN 171 AND 185 THEN 5
                WHEN weight BETWEEN 186 AND 205 THEN 6
                WHEN weight BETWEEN 206 AND 265 THEN 7
                ELSE 8 END) as f_division
    FROM fa_fighters
    ORDER BY f_division ASC) as fighters using (fighter_id)
ORDER BY score DESC

我猜这与最后一个 order by 语句的位置有关,但我不知道该放在哪里,因为它在连接查询中不起作用。现在,此查询生成以下内容:

Rankings Table                   Compared with Scores
+------------+------+----------+ +-------+
| fighter_id | rank | division | | score |
+------------+------+----------+ +-------+
| 131        | 1    | a1       | | 0     |
| 23         | 2    | a1       | | 34    |
| 332        | 3    | a1       | | 4     |
| 1233       | 1    | a2       | | 0     |
| 643        | 2    | a2       | | 44    |
+------------+------+----------+ +-------+

编辑:我之前的查询没有使用联接,但仅按战斗机是否专业进行分组,并且该查询确实正确应用了排名,所以我怀疑它与联接有关。

编辑:这是我选择的输出

SELECT * ,
(SELECT CONCAT_WS(  ' ', f_name, l_name ) FROM fa_fighters WHERE fa_fighters.fighter_id = fa_rankings.fighter_id)
AS name,
(SELECT score FROM fa_scores WHERE fa_scores.fighter_id = fa_rankings.fighter_id) 
AS score
FROM  `fa_rankings` 
WHERE 1 
ORDER BY division, rank ASC 
LIMIT 0 , 30

选择结果

编辑:它是按分数排序记录,但在错误的位置;它在确定排名后按分数对它们进行排序,因此原始表按分数排序,但战士没有与它们相关联的正确排名。我需要查询在运行排名计数器并确定每个战士排名之前按分数对记录进行排序。这是在执行查询时应用 ORDER BY 的问题,我只是不知道该放在哪里。

4

2 回答 2

2

我发现将“ORDER BY score DESC”移动到连接的子查询中会产生正确的结果:

INSERT INTO fa_rankings (fighter_id, rank, division)
SELECT 
    fa_scores.fighter_id,
    @r := CASE WHEN @div <> f_division THEN 1 ELSE @r+1 END as f_rank,
    @div := f_division
FROM
    (SELECT @div := NULL) d,
    (SELECT @r := 0) r,
    fa_scores
JOIN (SELECT fa_fighters.fighter_id,
        CONCAT(
            IF(status = 'professional', 'a', 'b'),
            CASE
                WHEN weight < 126 THEN 0
                WHEN weight BETWEEN 126 AND 135 THEN '1'
                WHEN weight BETWEEN 136 AND 145 THEN '2'
                WHEN weight BETWEEN 146 AND 155 THEN '3'
                WHEN weight BETWEEN 156 AND 170 THEN '4'
                WHEN weight BETWEEN 171 AND 185 THEN '5'
                WHEN weight BETWEEN 186 AND 205 THEN '6'
                WHEN weight BETWEEN 206 AND 265 THEN '7'
                ELSE '8' END) as f_division
    FROM fa_fighters
    ORDER BY 
        f_division ASC,
        #the score column in the form of a subquery
        (select score from fa_scores where fa_scores.fighter_id = fa_fighters.fighter_id) DESC) 
    as fighters using (fighter_id)
于 2012-07-02T20:28:53.297 回答
0

看起来你的表 TABLE 有一个关于除法的索引,这就是输出看起来像这样的原因。

如果您在进行查询时没有指定order by,则该表将根本没有任何顺序,即使使用索引您也会有某种顺序,但在大型表中不是 100% 准确。

恢复,在您的查询中,您有一个带有顺序和连接的 INSERT,但这并不意味着当您创建时select.....,输出将与输入的顺序相同。我的建议是始终指定ORDER BY并具有唯一或主键(至少其中一个)。

于 2012-07-02T18:59:52.917 回答