Re: PG UPDATE question - Mailing list pgsql-general

From Tom Lane
Subject Re: PG UPDATE question
Date
Msg-id 25877.985273887@sss.pgh.pa.us
Whole thread Raw
In response to PG UPDATE question  (Jeff Butera <jbutera@linus.highpoint.edu>)
List pgsql-general
Jeff Butera <jbutera@linus.highpoint.edu> writes:
> UPDATE STUDENT_ANSWERS SET CORRECT=QA.CORRECT
>   FROM QUESTION_ANSWERS QA,STUDENT_ANSWERS SA
>   WHERE SA.CORRECT=null
>     AND SA.QUESTIONS_ID=QA.QUESTIONS_ID
>     AND SA.ANSWERS_ID=QA.ANSWERS_ID;

What you've got here is a three-way join between QUESTION_ANSWERS and
two instances of STUDENT_ANSWERS (the update target and the SA alias).
I doubt that's what you want.  FROM in update should only be used for
*additional* tables.  The target table has no alias and must be spelled
out:

UPDATE STUDENT_ANSWERS SET CORRECT=QA.CORRECT
  FROM QUESTION_ANSWERS QA
  WHERE STUDENT_ANSWERS.CORRECT=null
    AND STUDENT_ANSWERS.QUESTIONS_ID=QA.QUESTIONS_ID
    AND STUDENT_ANSWERS.ANSWERS_ID=QA.ANSWERS_ID;

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: OID as Primary Key
Next
From: Tom Lane
Date:
Subject: Re: non-existent user "28"