[乐意黎原创]MySQL 中 You can't specify target table 'table_name' for update in FROM clause错误解决办法

如下图,当执行更新时

update `table_aerchi` as a
    set `biaohao` = (
        select count(*) from `table_aerchi` as b
        where b.`cuncode` ='532523101003' and b.`cunxuhao` ='01'
    )
where a.`cuncode` ='532523101003' and a.`cunxuhao` ='01'

抛错误,即: 失败原因:You can't specify target table 'outer_tb' for update in FROM clause

解决方法:select的结果再通过一个中间表select多一次,就可以避免这个错误

修改如下, table_aerchi 再用一次, 即用  (select * from `table_aerchi`) 来表示 table_aerchi

update `table_aerchi` as a
    set `biaohao` = (
        select count(*) from (select * from `table_aerchi`) as b
        where b.`cuncode` ='532523101003' and b.`cunxuhao` ='01'
    )where a.`cuncode` ='532523101003' and a.`cunxuhao` ='01'

执行后,成功。 提示: 

执行成功,影响行数:[1],耗时:[141ms.]

乐意黎

2019-07-21

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值