MySQL在update语句中选择

时间:2013-06-30 22:20:06

标签: mysql

这个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

我想为UPDATE(来自duel_player表的字段)创建MySQL语句,这个id_duel_player返回这个select语句。

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

我想要最有效,最快捷的方法。

谢谢

1 个答案:

答案 0 :(得分: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 ...)),但我发现它对于包含数百行的结果集不可靠。 (不可靠=突然没有找到匹配,不明白为什么,我没有调查过。)