Mysql -- You can't specify target table 'address' for update in FROM clause
Posted 每天多帅一点点
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql -- You can't specify target table 'address' for update in FROM clause相关的知识,希望对你有一定的参考价值。
做地址管理时,需要先根据要设为默认的地址的用户将用户的其他地址都设置为非默认
需要select出用户id然后update
原语句
update address set isdeafult = 0 where user_id = (select user_id from address where id = ?)
报错 -- You can‘t specify target table ‘address‘ for update in FROM clause
大意是不能先select出同一表中的某些值,再update这个表(在同一语句中)
修改后的语句如下
UPDATE address a INNER JOIN (SELECT user_id FROM address WHERE id = #{id}) c SET a.isdeafult = 0 WHERE a.user_id = c.user_id
解析
select * from address a INNER JOIN
(SELECT user_id FROM address WHERE id = 1) c WHERE a.user_id = c.user_id
的内容完全等于 select * from address ,本质上就是用inner join做了个中间表查询
注 : 只有在mysql中才有这个错误
以上是关于Mysql -- You can't specify target table 'address' for update in FROM clause的主要内容,如果未能解决你的问题,请参考以下文章
MySQL : You can't specify target table 'Person' for update in FROM clause
mysql中You can't specify target table for update in FROM clause
Mysql -- You can't specify target table 'address' for update in FROM clause
mysql:You can't specify target table 'bpm_tksign_data' for update in FROM clause
mysql uodate 报错 You can't specify target table '**' for update in FROM clause
mySQL: delete 语句报错 You can't specify target table 'student' for update in FROM clause(示例