Jeb
Jeb

Reputation: 81

How to Update a SQL table using Union

So I have 2 tables and I want to UPDATE Table 1 so that it is the Union of Table 1 and Table 2. Any suggestions?

Example Image

Upvotes: 1

Views: 1728

Answers (1)

GMB
GMB

Reputation: 222482

It looks like you just want to insert rows of table2 into table1. If so:

insert into table1 (col1, col2)
select col1, col2 from table2

You might be looking to a more subtle logic, like,: insert names that do not exist, and update values on name that exist already. If so, I would recommend on conflict. For this to work, you need a unique constraint on table2(col1), and then:

insert into table1 (col1, col2)
select col1, col2 from table2
on conflict (col1) do update set col2 = excluded.col2

Upvotes: 1

Related Questions