3

I have 3 tables and I want to update table1 (column status) with information (column status) from table2. the link between table1 and table2 is in the table table_con

http://www.sqlfiddle.com/#!15/6ce460/4

I thought about a join and using the result of the join to update table1

select t1.status as t1status,t2.status as t2status,t1.p_id as t1pid, t2.x_id as t2xid
   from table1 t1
      JOIN table_con tc
      ON t1.p_id = tc.p_id

      JOIN table2 t2 
      ON t2.x_id = tc.x_id;

The join works so far, but I don't know how to continue, and the query should work in psql. thanks

1
  • What columns do you want to update? Commented Apr 6, 2016 at 22:05

1 Answer 1

7

In Postgres, you can express joins in the update statement:

update table1 t1 
   set ?? = ??
   from table_con tc join
        table2 t2 
        on t2.x_id = tc.x_id
   where t1.p_id = tc.p_id;

Fill in the set column with the column and value you wnt to set.

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

2 Comments

What if t1 has to be joined with both tc and t2?
@AMJ . . . Then you would add another condition in the WHERE clause.

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.