MERGE INTO比對不同資料

2021-10-06 15:31:53 字數 2432 閱讀 9306

create table table_a(

id varchar(20),

name varchar(20),

*** varchar(20),

age varchar(20),

loaddate varchar(20)

);create table table_b(

id varchar(20),

name varchar(20),

*** varchar(20),

age varchar(20),

loaddate varchar(20)

);truncate table table_a;

truncate table table_b;

insert into "ora_oauth"."test_a" ("id", "name", "***", "student_no", "loaddate", "rowid") values ('1', '小明', '男', '27', '20200520', 'aaacokaaeaaaawjaaa');

insert into "ora_oauth"."test_a" ("id", "name", "***", "student_no", "loaddate", "rowid") values ('2', '小紅', '女', '28', '20200520', 'aaacokaaeaaaawjaab');

insert into "ora_oauth"."test_a" ("id", "name", "***", "student_no", "loaddate", "rowid") values ('4', '王五', '男', '30', '20200520', 'aaacokaaeaaaawjaac');

insert into "ora_oauth"."test_b" ("id", "name", "***", "student_no", "loaddate", "rowid") values ('1', '小明', '男', '27', '20200529', 'aaacolaaeaaaawraaa');

insert into "ora_oauth"."test_b" ("id", "name", "***", "student_no", "loaddate", "rowid") values ('2', '小紅', '女', '29', '20200529', 'aaacolaaeaaaawraab');

insert into "ora_oauth"."test_b" ("id", "name", "***", "student_no", "loaddate", "rowid") values ('3', '小張', '男', '24', '20200529', 'aaacolaaeaaaawraac');

commit;

merge into table_a a1

using (select id,name,***,student_no,loaddate from table_b a

where not exists(select * from table_a b

where a.id = b.id

and a.name = b.name

and a.*** = b.***

and a.age = b.age)) b1

on (a1.id = b1.id)

when matched then

update set a1.name = b1.name,

a1.*** = b1.***,

a1.age = b1.age,

a1.loaddate = b1.loaddate

when not matched then

insert(id,name,***,age,loaddate) values(b1.id,b1.name,b1.***,b1.age,b1.loaddate);

commit;

最後預期結果:新增小張資料與更新小紅年齡與日期

1    小明    男    27    20200520

2    小紅    女    29    20200529

4    王五    男    30    20200520

3    小張    男    24    20200529

--比對test_b多出的資料,與不一樣的資料

(select id,name,***,student_no,loaddate from test_b a

where not exists(select * from test_a b

where a.id = b.id

and a.name = b.name

and a.*** = b.***

and a.student_no = b.student_no));

結果:3    小張    男    24    20200529

2    小紅    女    29    20200529

merge into合併資料

語法 其中as可以省略 merge into table name as table alias using table view sub query as alias on join condition when matched then update set col1 col val1,col2...

資料庫比對

如何快速進行兩個資料庫結構的比對,以下三步可以輕鬆完成 一 建立資料庫連線 create public database link dl system connect tosystem identified by oracle using description address list addre...

資料倉儲的資料比對

1.資料量的比對 select count 2.資料內容全字段的比對,難度比較大。mysql 資料倉儲 full join結果作為 t表 補資料 select from t where bid is null 拿到a表字段的資料 拼接sql插入資料倉儲 刪資料 select from t where...