master_table:
_id | zipcode |
---|---|
123 | 100 |
456 | 200 |
temp_table:
_id | zipcode |
---|---|
123 | 111 |
245 | 222 |
master_table (new results must be upserted in master_table)
_id | zipcode |
---|---|
123 | 111 |
456 | 200 |
my end goal is to insert records from temp_table to master_table and update existing records in master_table found in temp_table.
I have just given a sample table, It is not just the zipcode column which needs to be updated. there are many such columns (20+), all of them need to be updated.
just for a note : both the tables have same no. of columns.
2
Answers
Use an update join:
You can do it using
WITH
to get data that need to be updated only.This approach lets you develop and test your select query and in two steps convert it to the update query.
Demo here
To update multi columns just add them in
inner join
, and of course add them in theSET
clause :Demo here