涉及兩張表的數據更新

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中測試通過

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章