我正在使用 Postgresql 数据库。我有两个表中的数据。表 A 有 10 条记录,表 B 有 5 条记录。
我想将表 A 的数据复制到表 B 但只复制新条目(5 条记录)并忽略重复项/已经存在的数据
我想将数据从表 A 复制到表 B,其中表 B 将有 10 条记录(表 A 中的 5 条旧记录 + 5 条新记录)
你能帮我看看如何做到这一点吗?
假设 id 是您的主键,并且表结构相同(两个表分别具有公共列作为列数和数据类型),请使用not exists
:
insert into TableB
select *
from TableA a
where not exists ( select 0 from TableB b where b.id = a.id )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句