在Mysql用In(子查询)来更新数据表时,如下:
1.update te set sex=0 where id in( select t.id from te t where t.parent_id =1)
报You can't specify target table for update in FROM clause错误
但是用如下语句却不会:
2.update te set sex=0 where id in(1,2)
百度了下,大家都说原因是第一个语句不能先select出同一表中的某些值,再update这个表(在同一语句中)。
改成用临时表如下,就可以避开这个问题:
3.update te set sex=0 where id in (select id from ( select t.id from te t where t.parent_id =1) t)
这样就可以更新了