I have table A
that contains about 10 millions rows and Table B
which contains some updated information for some rows in table A
and also contains new rows that don't exist in table A
.
I want to update table A
using table B
and at the same time insert rows that have no match in table A
.
I found many answers like the solution below but it seems like they all miss the inserting part that I'm looking for.
UPDATE A
SET code = B.code
FROM B
WHERE A.id = B.id
Use two queries:
update a
set code = b.code
from b
where a.id = b.id;
insert into a (id, code)
select id, code
from b
where not exists (select 1 from a where a.id = b.id);
You can also use on conflict
insert into a (id, code)
select b.id, b.code
on conflict on constraint a_id
do update set code = b.code;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.