【MySQL】Error Code: 1093. You can‘t specify target table ‘xxx‘ for update in FROM clause

本文讲述了在MySQL中遇到的错误,如‘self-check update’报错,如何通过修改SQL语句、使用别名及处理safe update模式来解决。作者逐步解析了错误原因并提供了相应的解决方案,最终成功执行了更新操作。
摘要由CSDN通过智能技术生成

1. 报错信息:

Error Code: 1093. You can't specify target table 'self_check' for update in FROM clause

错误的sql语句:

update self_check set calculated=1 where self_check.id in (select distinct self_check.id from self_check, self_result where self_check.selfId=self_result.selfId and self_check.chainId=self_result.chainId order by self_check.id)

错误原因:

不能根据某个表select的字段去update这个表(对同一个表的where和update不能出现在同一语句中)

可以再套一层select,即:update self_check set calculated=1 where self_check.id in (select * from (select distinct self_check.id from self_check, self_result where self_check.selfId=self_result.selfId and self_check.chainId=self_result.chainId order by self_check.id))

参考文章:https://blog.csdn.net/chinafire525/article/details/91045069

 

2. 改成上述sql后出现了新的问题

报错信息:

Error Code: 1248. Every derived table must have its own alias
错误原因:

对于查询得到的派生表要起个别名

将sql改成:update self_check set calculated=1 where self_check.id in (select * from (select distinct self_check.id from self_check, self_result where self_check.selfId=self_result.selfId and self_check.chainId=self_result.chainId order by self_check.id) as checkIds)

 

3. 改成上述sql后又出现了新问题

报错信息:

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.  To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
错误原因:

为了防止没有设置where就update所有数据(防止update误操作),workbench默认会设置为safe update模式。错误信息说的是在update table的时候where没有限定主键,使得update不能根据主键来更新,存在一定的问题。

其实我这个self_check.id是self_check表的主键,但是可能因为它后续接着的sql语句是select *,导致mysql判断为没有对主键做限定。这种情况按照上面的错误信息把safe update模式关闭即可:File->Preferences->SQL Editor->Safe Updates取消勾选->OK

4. 终于成功执行了,可喜可贺*★,°*:.☆( ̄▽ ̄)/$:*.°★* 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值