1

I have two tables:

Connections:

id  user_id  connection_info
------------------------------
 1    1      ...
 2    1      ...
 3    2      ...

Lists:

id  connection_id  name
-----------------------
1        1         ...
2        2         ...
3        1         ...

I currently have user_id's in the lists.connection_id column. I would like to join the lists table with the connections table by connections.user_id = lists.connection_id and then replace the lists.connection_id with the corresponding id from the connections table.

1
  • 2
    so did you try something and fail? if so share your code/errors please. if not, attempt a solution and share it to give folks something to work with. Commented Oct 5, 2017 at 9:17

1 Answer 1

1

You could use UPDATE FROM like this:

update l
set l.connection_id = c.id 
from connections c join lists l on c.user_id = l.connection_id

Initially you would want to test what you are going to update, running a SELECT statement:

select l.connection_id as con_old
     , c.id as con_new
     , ... (other cols you might want to check)
from connections c join lists l on c.user_id = l.connection_id
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.