MySQL在执行更新或者删除操作时,当条件还是该表查询出来的时候,会报
[ERR] 1093 - You can’t specify target table ‘student_info’ for update in FROM clause
这可能更新的表与查询的表为同一表会存在嵌套递归,MySQL会报上述错误。
如:
update student_info
set state = 0
where id in (select id from student_info
where name like '%李林%'
and class_id in ('class_2','class_4','class_5','class_7','class_8'));
这时就会报 [ERR] 1093 - You can’t specify target table ‘student_info’ for update in FROM clause
解决办法是:
在同表的条件处做一层等价表的嵌套查询,这样可以避免条件也是要变更的表,具体如下:
update student_info
set state = 0
where id in (select t.id from (
(select id from student_info
where name like '%李林%'
and class_id in ('class_2','class_4','class_5','class_7','class_8'))t
);