1

I have a table that has two primary keys, first_name and last_name and also has about 10 other columns. I want to update it using another table that has the exact same schema but many have new values to add or update to the old table. What is the correct SQL query?

Here is a picture of what I am trying to do

The code I have so far is:

UPDATE OLD_DATA AS old
SET val_1 = new.val_1 ,
    val_2 = new.val_2 ,
FROM NEW_DATA AS new
WHERE old.first_name = new.first_name
AND   old.last_name  = new.last_name 

But this doesn't add new data, it only updates old data.

3
  • You don't want to specify columns to update? Commented Mar 19, 2018 at 20:48
  • No, I want it to update all the columns. Commented Mar 19, 2018 at 22:00
  • Please read meta.stackoverflow.com/questions/285551/… and the accepted answer Commented Mar 20, 2018 at 14:46

1 Answer 1

1

Here is the documentation on postgres updates.

update old_data set val_1 = 5 and val_2 = 1
where first_name = 'donald' and last_name = 'duck'

Do the same for each row.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.