s

postgresql update table from another table with columns from second table edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 03 February 2022 | 2294

Postgresql update table a from another table b with columns from table b where table a column matches table b column. This can be done using an update from select query

Solution

I have two tables deleteme_dest, deleteme_src both having the same columns resourceid & tags.

first, create the tables and then populate the data using the below query

insert into deleteme_dest(resourceid,tags) values('1','a'), ('1','b'), ('2','c'), ('1','d'),('2','e'),('3','a'),('4','y')

insert into deleteme_src(resourceid,tags) values('1','srca'), ('2','srcb')

now you can run the below query to copy the data from src to destination.

update deleteme_dest set tags = deleteme_src.tags from deleteme_src 
where deleteme_dest.resourceid = deleteme_src.resourceid

Once the data has been copied. you will get the below output.