oracle中,如果跨两个表进行更新,sql语句写成这样,oracle 不会通过。查了资料,sql语句需要这样写才行
前提条件:
表info_user中有字段id和name,字段id为索引
表data_user_info中有字段id和name,字段id为索引
其中表info_user中字段id和表data_user_info中字段id数值一致。
要求实现:
更新表info_user中的字段name 与表data_user_info中字段name一致。
实现的sql语句为:
update info_user i set (i.name) = (select d.name from data_user_info d where d.id = i.id)
where i.id = (select d.id from data_user_info d where d.id = i.id);
commit;
-------------------------分割线-------------------------
oracle中,如果跨两个表进行更新,sql语句写成这样
update table a set a.id=table2.id where a.name = table2.name
oracle 不会通过。查了资料,,sql语句需要这样写才行
update table a set a.id=(select b.id from table2 b where b.name = a.name ) where exist (select 1 from table2 b.name=a.name)
更新多个字段也可以
update table a set a.id=(select b.id from table2 b where b.name = a.name ),a.code=(select b.code from table2 b where b.name = a.name ) where exist (select 1 from table2 b.name=a.name)