当前位置:操作系统 > Unix/Linux >>

涉及两张表的数据更新

涉及两张表的数据更新
 
oracle中只更新两张表对应数据的方法
 
前提:有表table1和table2.表结构都是id,name。
 
问题:当table1的id和table2的id相同时更新table1的name为table2的name
 
解决方案:
 
update table1 set name = (select name from table2 where table1.id = table2.id) where exists (select 1 from table2 where table1.id = table2.id)
 
解释:
 
where条件是防止table1中存在的id而table2中不存在的id所对应的数据被清空
 
注:以上语句仅在oracle11g中测试通过
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,