mysql中You can't specify target table for update in FROM clause
错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。
UPDATE bus_clew SET clew_status = '3' WHERE
id IN (
SELECT
c.id
FROM
bus_clew c,
bus_clew_keeper k
WHERE
c.id = k.clew_id
AND c.clew_status = 0
AND c.is_del = 0
AND EXISTS (
SELECT
1
FROM
bus_clew_track t
WHERE
t.clew_id = c.id
AND t.keeper_id = k.keeper_id
AND t.is_del = 0
)
);
要改成下面:
UPDATE bus_clew SET clew_status = '3' WHERE
id IN (
select c1.id from (
SELECT
c.id
FROM
bus_clew c,
bus_clew_keeper k
WHERE
c.id = k.clew_id
AND c.clew_status = 0
AND c.is_del = 0
AND EXISTS (
SELECT
1
FROM
bus_clew_track t
WHERE
t.clew_id = c.id
AND t.keeper_id = k.keeper_id
AND t.is_del = 0
)) c1
);
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。