简体   繁体   English

MySQL在更新语句中选择

[英]MySQL select in update statement

This MySQL statement give me all id_duel_player for player with id_player=30 and it work fine. 该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

I want to make MySQL statement for UPDATE (fields from duel_player table) all of this id_duel_player that returns this select statement. 我想使所有返回此选择语句的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)

I want most effective and fastest way to do this. 我想要最有效,最快的方法。

Thanks 谢谢

For 200-400 rows it's likely fastest to create a temporary table with the results, and then do the UPDATE with a join: 对于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

For smaller result sets you may find the IN operator sufficiently fast ( ... WHERE id_duel_player IN (SELECT ...) ), but I've found it unreliable for result sets with hundreds of rows. 对于较小的结果集,您可能会发现IN运算符足够快( ... WHERE id_duel_player IN (SELECT ...) ),但是我发现它对于具有数百行的结果集并不可靠。 (Unreliable = suddenly no matches are found, no idea why, I haven't investigated.) (不可靠=突然找不到匹配项,不知道为什么,我没有调查。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM