聯接查詢更新

Oracle 實現多表參照更新

參照T2 修改 T1

表1
image
表2
image

  • 方法1
update student set grade=(select grade from gradedetial where sid=student.id and rownum=1)
where exists(select count(1) from gradedetial where sid=student.id)

如果同時更新多個字段可以參照語法:
update tb1 t1 set(字段1,字段2,...)=(select 字段1,字段2... from tb2 t2 where t2.id=t2.id)
where exists(select count(1) from t2 where t2.id=t1.id)

  • 方法2 內聯視圖更新(要求取數據的表即T2,該字段必須是主鍵或者有唯一約束),上表不具備這個條件,因此這個方法在這裏會失敗!
update (select a.grade grade1,b.grade grade2 from student a inner join gradedetial b
on a.id=b.sid) t
set grade1=grade2;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章