0

I want to do a select and update in a single query in PostgreSQL 9.4. I used below command for that

UPDATE (SELECT t2.pid, t1.pid as newid
 FROM table1 AS t1
 INNER JOIN table2 AS t2 
 ON t1.uid = t2.uid
 ) AS t
SET t.newid = t.pid';

When executing the above, I'm getting below error.

ERROR:  syntax error at or near "("
LINE 2:                 (SELECT t2.pid, t1.pid as n...
                        ^
SQL state: 42601
Character: 25

I executed

SELECT t2.pid, t1.pid as newid
 FROM table1 AS t1
 INNER JOIN table2 AS t2 
 ON t1.uid = t2.uid 

part separately and I worked fine. Do I miss something in the update query?

1 Answer 1

1

As documented in the manual the target in an UPDATE can not be a SELECT statement.

But you can use a FROM clause to join another table to the actual target table:

UPDATE t1
   set pid = t2.pid
FROM table2 as t2
WHERE t2.uid = t1.uid;
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.