假如有一张人员表sys_user(10000人)
id |
name |
age |
和一张临时表sys_temp(100人)
id |
name_new |
age |
现在要将临时表中的name_new 字段的值更新到sys_user的name字段中去,用两表的id作为关联条件:
错误的sql
update sys_user a set name=(select name_new from sys_temp b where a.id = b.id)
原因:在oracle中,如果update后边没有where条件,会把整个表全部更新,那么不符合子查询规则的数据,会将name字段的变成空。
正确sql
可以在后边加上exists条件来进行限制
update sys_user a
set name=(select name_new from sys_temp b where a.id = b.id)
where exists(select 1 from sys_temp temp where temp.id = a.id );