在更新表数据的时候,我们时常需要把另外一个表的关联值的数据更新到当前表,此时就需要用到子查询了:

MS SQL Server的写法:

update child set parent_name =p.name from child c,parent p where c.parent_id=p.id
或者
update child set parent_name =(select name from parent p where p.id=parent_id)

但是MySQL执行如上语句则会报错:
Error Code: 1093. You can´t specify target table ´xxx´ for update in FROM clause

MySQL不支持子查询更新,MySQL我们应该这样写:

update child c inner join parent p on p.id=c.parent_id set c.parent_name=p.name;