搜索
您的当前位置:首页正文

Oracle中 如何用一个表的数据更新另一个表中的数据

来源:知库网
建表语句:

create table table1(
idd varchar2(10) ,
val varchar2(20)
);
create table table2(
idd varchar2(10),
val varchar2(20)
);

插入数据:

insert into table1 values ('01','1111');
insert into table1 values ('02','222');
insert into table1 values ('02','2222');
insert into table1 values ('03','3333');
insert into table1 values ('04','4444');
insert into table1 values ('06','6666');
commit;
insert into table2 values ('01','aaaa');
insert into table2 values ('02','bbbb');
insert into table2 values ('03','cccc');
insert into table2 values ('04','dddd');
insert into table2 values ('05','eee');
insert into table2 values ('05','eeee');
commit;

2表如下:
image.png image.png
要将 table2中idd - val 的值,赋值给table1对应的 idd - val;

注意:

  • table1中 有 2个 idd 为 02,val 不同;
  • table2中 有 05,table1中没有;
  • table1中 有 06,table2中没有。

sql语句:

  1. 通过子查询 ,直接 update 更新,如下:
    update table1 set table1.val = (select val from table2 where table1.idd = table2.idd);


    image.png
  • 问题:对于 table1中idd存在,table2中不存在,val变成了null;
  1. 改进,加入限制条件,对于 table1 中有,但是table2中不存在的idd,不做修改;
    update table1 set val = (select val from table2 where table1.idd = table2.idd)
    where exists (select 1 from table2 where table1.idd = table2.idd)


    image.png
  • 但上述2种写法,遇到table2中继续插入数据,
    insert into table2 values ('03','ccc');
    即table2 中有一个idd对应多个val,并且在table1中有对应idd时。
  • 执行后会报错:

    image.png
  1. 使用merge,如下:
    merge into table1
    using table2
    on (table1.idd = table2.idd)
    when matched then
    update set table1.val = table2.val
  • 遇到 table2 中有一个idd对应多个val,并且在table1中有对应idd时,报错如下:
    image.png
  1. 在3的基础上,加入限制条件;
    merge into table1
    using (select t.idd ,max(t.val) m from table2 t group by t.idd)table2
    on (table1.idd = table2.idd)
    when matched then
    update set table1.val = table2.m
  • 上述写法在 using后面构造了一个新的table2,group by idd,但一定要对val做出处理,如果是varchar类型,可以选择 max,min等函数,如果number类型,可以使用sum,avg等函数,总之,要对val做出筛选,新的table2是一个idd对应一个val。
Top