mysql You can/'t specify target table for update in FROM clause

2017-01-09 14:09:19来源:作者:youyanweixiao人点击

mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。例如下面这个sql:

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不会出现此问题。

相关文章

    无相关信息

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台