【发布时间】:2016-10-12 19:53:08
【问题描述】:
以下是我的场景:
我有 2 个登陆表 source_table 和 destination_table。
我需要一个查询/查询,它将使用新行以及源表中的更新行更新目标表。
样本数据为:
source table:
id name salary
1 P1 10000
2 P2 20000
target table:
id name salary
1 P1 8000
预期的输出应该是:
target table:
id name salary
1 P1 10000 (salary updated)
2 P2 20000 (new row inserted)
这似乎不起作用:
select * from user_source
except
select * from user_target as s
INSERT INTO user_target (id, name, salary)
VALUES (s.id, s.name, s.salary) WHERE id !=s.id
UPDATE user_target
SET name=s.name, salary=s.salary,
WHERE id = s.id
【问题讨论】:
-
@a_horse_with_no_name 完成!
标签: postgresql upsert