user
user

Reputation: 49

update column value from another table column value in postgres

I have two tables as below:

i want to update a icon value in xyz table with specific id. For this i am using below query.

update xyz t set image = (select image from abc t1 where id = t.abcId);

But this update all the values. Can any one please update this.

Upvotes: 2

Views: 110

Answers (2)

Hiren Patel
Hiren Patel

Reputation: 1157

I suggest this query:

update xyz t 
set image = 
    (select image from abc t1 where t1.id = t.abcId) 
where t.id="**ID That You Want To Update**";

Upvotes: 1

Laurenz Albe
Laurenz Albe

Reputation: 247950

You forgot to add a WHERE clause to the UPDATE statement, that's why all rows are updated.

Try adding the following at the end of the statement:

WHERE t.id = 42

Then only the xyz with id 42 will be updated.

Upvotes: 2

Related Questions