博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
You can't specify target table 'table' for update in FROM clause
阅读量:7164 次
发布时间:2019-06-29

本文共 518 字,大约阅读时间需要 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

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

转载地址:http://avxwm.baihongyu.com/

你可能感兴趣的文章