0

当一个新的拳击手添加到我的数据库时,我的应用程序中有以下查询来修复任何未知的拳击手 ID。为了使系统中的其他查询和功能更容易,添加回合时,boxer_1ID总是小于boxer_2ID。

下面的函数似乎可以完美地更新/切换boxer_1andboxer_2字段,但是boxer_1_weightandboxer_2_weight值得到奇怪的结果,我不知道为什么?

"UPDATE bouts b1, bouts b2 SET b1.unknown_global_id = 0,
                    b1.winner = IF(b2.winner = -3, IF(b1.boxer_1 = 0, '" . $boxer_id . "', b1.boxer_1), IF(b2.winner = -2, IF(b1.boxer_2 = 0, '" . $boxer_id . "', b1.boxer_2), b1.winner)),
                    b1.boxer_1 = IF(b1.boxer_1 = 0, IF('" . $boxer_id . "' > b1.boxer_2, b1.boxer_2, '" . $boxer_id . "'), IF(b1.boxer_1 < '" . $boxer_id . "', b1.boxer_1, '" . $boxer_id . "')), 
                    b1.boxer_2 = IF(b1.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1, '" . $boxer_id . "'), IF(b1.boxer_2 > '" . $boxer_id . "', b1.boxer_2, '" . $boxer_id . "')),
                    b1.boxer_1_weight = IF(b2.boxer_1 = 0, IF('" . $boxer_id . "' > b2.boxer_2, b2.boxer_2_weight, b2.boxer_1_weight), IF(b2.boxer_1 < '" . $boxer_id . "', b2.boxer_1_weight, b2.boxer_2_weight)),
                    b1.boxer_2_weight = IF(b2.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1_weight, b2.boxer_2_weight), IF(b2.boxer_2 > '" . $boxer_id . "', b2.boxer_2_weight, b2.boxer_1_weight))
                    WHERE b1.unknown_global_id = '" . (int)$global_id . "'"

boxer_1/boxer_2字段为 INT(11),boxer_1_weight/boxer_2_weight字段为十进制(10,1)

例如,如果权重字段以权重 186.5 和 199.7 开头,则在运行查询后它们都以 239.8 结束,但拳击手 ID 是正确的。

4

1 回答 1

1

您需要一个连接条件来避免将 中的选定行与 中b1的所有其他行进行比较b2

"UPDATE bouts b1
        JOIN bouts b2 ON b1.bout_id = b2.bout_id
        SET b1.unknown_global_id = 0,
            b1.winner = IF(b2.winner = -3, IF(b1.boxer_1 = 0, '" . $boxer_id . "', b1.boxer_1), IF(b2.winner = -2, IF(b1.boxer_2 = 0, '" . $boxer_id . "', b1.boxer_2), b1.winner)),
            b1.boxer_1 = IF(b1.boxer_1 = 0, IF('" . $boxer_id . "' > b1.boxer_2, b1.boxer_2, '" . $boxer_id . "'), IF(b1.boxer_1 < '" . $boxer_id . "', b1.boxer_1, '" . $boxer_id . "')), 
            b1.boxer_2 = IF(b1.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1, '" . $boxer_id . "'), IF(b1.boxer_2 > '" . $boxer_id . "', b1.boxer_2, '" . $boxer_id . "')),
            b1.boxer_1_weight = IF(b2.boxer_1 = 0, IF('" . $boxer_id . "' > b2.boxer_2, b2.boxer_2_weight, b2.boxer_1_weight), IF(b2.boxer_1 < '" . $boxer_id . "', b2.boxer_1_weight, b2.boxer_2_weight)),
            b1.boxer_2_weight = IF(b2.boxer_2 = 0, IF('" . $boxer_id . "' < b2.boxer_1, b2.boxer_1_weight, b2.boxer_2_weight), IF(b2.boxer_2 > '" . $boxer_id . "', b2.boxer_2_weight, b2.boxer_1_weight))
        WHERE b1.unknown_global_id = '" . (int)$global_id . "'"
于 2013-06-01T10:32:41.597 回答