在Where子句中使用查询更新表

时间:2015-01-15 20:42:34

标签: mysql sql

我在下面的查询中做错了什么

Error Message :  You can't specify target table 'cp_asseteventinquiry' for update in FROM clause

查询:

UPDATE cp_asseteventinquiry
SET Due_Date= curdate()
WHERE TopLevelAsset_Num =
(
   SELECT Distinct(TopLevelAsset_Num) FROM cp_asseteventinquiry
   WHERE cp_asseteventinquiry.History='NO'
   AND NOT EXISTS(SELECT Scheduled_Date FROM flightlogs
   WHERE flightlogs.Asset=cp_asseteventinquiry.TopLevelAsset_Num 
   AND flightlogs.Scheduled_Date=date_format (curdate(), '%m/%d/%Y'))
);

1 个答案:

答案 0 :(得分:0)

用这样的SELECT包装您的内部查询:

UPDATE cp_asseteventinquiry
SET Due_Date= curdate()
WHERE TopLevelAsset_Num = (
    SELECT * 
    FROM ( 
          SELECT Distinct(TopLevelAsset_Num)
          FROM cp_asseteventinquiry
          WHERE cp_asseteventinquiry.History='NO'
            AND NOT EXISTS( SELECT Scheduled_Date 
                            FROM flightlogs 
                            WHERE flightlogs.Asset=cp_asseteventinquiry.TopLevelAsset_Num
                              AND flightlogs.Scheduled_Date=date_format (curdate(), '%m/%d/%Y')
            )
    ) AS t)