0

I have a table t1 with columns account_id and user_id, and another table users with columns id and account_id. I want to populate t1.account_id with the values of users.account_id.

For instance having

t1
user_id account_id
1 NULL
2 NULL

and

users
id account_id
1 10
2 11

I would like to populate t1.account_id with these values:

t1
user_id account_id
1 10
2 11

I've seen this answer for MySQL but can't have it working with PostgreSQL.

asked Feb 18, 2015 at 22:20
1

1 Answer 1

2

update documentation

UPDATE t1 AS a SET
 account_id = b.account_id
FROM users b
WHERE a.user_id = b.id;

The key is that you need to make sure you have a link between the table you're updating (aliased as "a" in my example) and the table you're using .. which is done via the WHERE clause... a.user_id = b.id

answered Feb 18, 2015 at 22:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.