Oracle 多表关联update
发布时间:2020-12-12 13:57:26 所属栏目:百科 来源:网络整理
导读:多表关联执行update 1.仅在where子句中连接 --table2中全部为优秀学生,将table1中的优秀学生的成绩更新为优update table1 t1 set t1.grade = '优'where exists ( select 1 from table2 t2 where t2.id = t1.id); 2.update的值来自另一个表 看完以下例子,你
多表关联执行update1.仅在where子句中连接--table2中全部为优秀学生,将table1中的优秀学生的成绩更新为优 update table1 t1 set t1.grade = '优' where exists ( select 1 from table2 t2 where t2.id = t1.id ); 2.update的值来自另一个表看完以下例子,你就会明白了。 示例:表t_tst_bkin存储了银行联行行号及其上级机构的联行行号,现在需要将每个银行的总部的联行行号维护进sup_lbnk_no字段 初始化sql: --创建表 create table t_tst_bkin ( lbnk_no varchar2(12) not null enable,lbnk_nm varchar2(256) not null enable,up_lbnk_no varchar2(12) not null enable,sup_lbnk_no varchar2(12),primary key (lbnk_no) ); comment on table t_tst_bkin is '联行行号信息表'; comment on column t_tst_bkin.lbnk_no is '支付行号'; comment on column t_tst_bkin.lbnk_nm is '机构名称'; comment on column t_tst_bkin.up_lbnk_no is '上级机构支付行号'; comment on column t_tst_bkin.sup_lbnk_no is '总部支付行号'; --初始化数据 insert into T_TST_BKIN(LBNK_NO,LBNK_NM,UP_LBNK_NO,SUP_LBNK_NO) values('104227600050','中国银行葫芦岛化工街支行','104222017850',''); insert into T_TST_BKIN(LBNK_NO,SUP_LBNK_NO) values('104227600068','中国银行葫芦岛龙港支行',SUP_LBNK_NO) values('104222017850','中国银行大连市分行','104100000004',SUP_LBNK_NO) values('104100000004','中国银行总行',''); 步骤一: --维护总部的sup_lbnk_no字段,条件为lbnk_no=up_lbnk_no update t_tst_bkin t set t.sup_lbnk_no = lbnk_no where t.lbnk_no = t.up_lbnk_no; 步骤二(此处用到了两表关联update,且update的数据来自另一个表): --将上级机构的sup_lbnk_no维护进该机构sup_lbnk_no update t_tst_bkin t set t.sup_lbnk_no = (select tt.sup_lbnk_no from t_tst_bkin tt where tt.sup_lbnk_no is not null and t.up_lbnk_no = tt.lbnk_no) where exists (select 1 from t_tst_bkin tt where tt.sup_lbnk_no is not null and t.up_lbnk_no = tt.lbnk_no) and t.sup_lbnk_no is null; 若存在多级机构,执行完步骤一,然后重复执行步骤二即可。 或许该例子存在其他便捷的实现该功能的sql,此处仅作为示例。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |