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

最近在执行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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值