1

这个 MySQL 语句为我提供了 id_player=30 的所有 id_duel_player 并且它工作正常。

SELECT   b.id_duel_player
FROM    duels a                            
        INNER JOIN duel_player b          
            ON a.id_duel = b.id_duel
WHERE   id_player = 30 
UNION ALL
SELECT  c.id_duel_player
FROM    duel_player c
        INNER JOIN
        (
            SELECT  aa.*
            FROM    duels aa
                    INNER JOIN duel_player bb
                        ON aa.id_duel = bb.id_duel
            WHERE   bb.id_player = 30
        ) d ON c.id_duel = d.id_duel AND c.id_player <> 30

我想为所有返回此选择语句的 id_duel_player 的 UPDATE(duel_player 表中的字段)创建 MySQL 语句。

UPDATE duel_player
SET num = 2,
    total = 5
WHERE (duel_player.id_duel_player = id_duel_player's from above SELECT statement)

我想要最有效和最快的方法来做到这一点。

谢谢

4

1 回答 1

1

对于 200-400 行,使用结果创建临时表可能最快,然后使用连接执行 UPDATE:

CREATE TEMPORARY TABLE id_duel_players AS
SELECT b.id_duel_player as id FROM duels a ...

UPDATE duel_player
JOIN id_duel_players ON duel_player.id_duel_player = id_duel_players.id
SET num = 2,
    total = 5

对于较小的结果集,您可能会发现该IN运算符足够快 ( ... WHERE id_duel_player IN (SELECT ...)),但我发现它对于具有数百行的结果集不可靠。(不可靠=突然找不到匹配项,不知道为什么,我没有调查。)

于 2013-06-30T22:48:24.010 回答