【发布时间】:2019-12-01 21:17:35
【问题描述】:
我有 table1 作为
我有table2
我需要用 table2 值更新 table1。两个表中相同的 mbr_id 记录应替换为 table2 值。 table2 中的新 mbr_id 应附加到 table1。
预期输出:
我的方法
- 从 table2 中分离现有的 mbr_id 和新的 mbr_id
create table set1 as select * from table2 where mbr_id not in (select mbr_id from table1)
create table set2 as select * from table2 where mbr_id in (select mbr_id from table1)
- 将 set1 附加到 table1
insert into table1 select * from set1
- 用 set2 更新 table1
UPDATE t1
FROM table1 t1,
set2 t2
SET
sales_bucket = t2.sales_bucket,
pro = t2.pro,
Region = t2.Region
where t1.mbr_id = t2.mbr_id
问题是:有没有更好的方法一次性完成同样的任务?
【问题讨论】:
-
是的,有。检查
MERGE语法。 -
我会试试@dnoeth