MYSQL错误代码: 1093 You can't specify target table 'sc' for update in FROM clause
今天写update语句时发现一个新的问题记录一下
意思是不能在同一语句中更新select出的同一张表元组的属性值
解决方法:将select出的结果通过中间表再select一遍即可。
仔细想想也是这样的,对同一张表查的同时更新会引起数据不一致的问题吧,但是将查询结果事先放到临时表中就不会有这个问题了。
这个是我们在使用update或者delete语句时,在where条件里面加入的子查询导致的。例如如下的update语句:
// An highlighted block
update table set type = 'int' where id in (
select id from product where first_name ='zhang');
修改上述语句为下面这样,该问题可以解决: