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

575 阅读1分钟

最近在执行mysql update语句时遇到这么一个问题:

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

我的sql语句是这样的(简化版):

update A 

set type= 0 

where id in 

(select a.id from A a,B b where a.bId= b.id and b.mId=#{mId} and a.tel=#{tel});

在做更新操作的时候,又做了一次表内查询,这显然会导致必要的字段被隐式复制到临时表中。

解决方案

多做一次A表的查询,为了让A的临时表不冲突,将sql语句改动一下:

update A

set type= 0

where id in

(select a.id from (select id,bId,tel from A) a,B b where a.bId= b.id and b.mId=#{mId} and a.tel=#{tel});

ps:这个新的查询语句,只将表A中的几个字段查出来作为临时表,而不是整个表A作为临时表

重新执行:


问题解决

end

您的点赞和关注是对我最大的支持,谢谢!