联接查询更新

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