MySql-Update:无法在FROM子句中指定更新的目标表

时间:2015-11-06 10:10:50

标签: mysql sql sql-update

我正在尝试对符合特定条件的值执行更新操作。我的表CAPD, CAMP, CAD。但是我得到错误

  

错误代码:1093。您无法为更新指定目标表'CAPD'   FROM子句

UPDATE CAPD SET CAPD.Is_Active = 1
WHERE CAPD.Per_Id IN (
    SELECT CAMP.Id

    FROM CAMP 

    INNER JOIN CAPD ON (
        CAPD.Per_Id = CAMP.Id 
        AND CAPD.Is_Active = 0 
    ) 
    INNER JOIN CAD ON (
        CAD.Id = CAPD.Deploy_Id 
        AND BINARY CAD.Access_Id = "486579446F6E277-4436F6E7665727449742E2E4C-4F4C203A5020584F586F"
    ) 

    WHERE CAMP.Serial = "ABC1230071"
)

2 个答案:

答案 0 :(得分:0)

您无法使用要在子查询中更新的目标表。如果您想使用它,则需要使用UPDATE CAPD INNER JOIN CAD ON ( CAD.Id = CAPD.Deploy_Id) SET CAPD.Is_Active = 1 WHERE CAPD.Per_Id IN (SELECT CAMP.Id FROM CAMP WHERE CAMP.Serial = "ABC1230071" ) and CAD.Access_Id = "486579446F6E277-4436F6E7665727449742E2E4C-4F4C203A5020584F586F" and CAPD.Is_Active = 0 。试试这样:

{{1}}

答案 1 :(得分:0)

  

您无法使用要在其中更新的目标表   子查询。如果你想使用它,你需要使用JOIN - 首先   由@Rahul Tripathi回答

您必须尝试此查询,然后才需要设置SET SQL_SAFE_UPDATES

SET SQL_SAFE_UPDATES=0;

UPDATE CAPD 
INNER JOIN CAD ON ( CAD.Id = CAPD.Deploy_Id) 

SET CAPD.Is_Active = 1

WHERE CAPD.Per_Id IN (
 SELECT CAMP.Id 
    FROM CAMP 
    WHERE CAMP.Serial = "ABC1230071"
)

AND  BINARY CAD.Access_Id = "486579446F6E277-4436F6E7665727449742E2E4C-4F4C203A5020584F586F";

SET SQL_SAFE_UPDATES=1;