Re: PG UPDATE question

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: jbutera(at)linus(dot)highpoint(dot)edu
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PG UPDATE question
Date: 2001-03-22 15:11:27
Message-ID: 25877.985273887@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jeff Butera <jbutera(at)linus(dot)highpoint(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Lockhart 2001-03-22 15:15:40 Re: Re: Call for platforms
Previous Message Tom Lane 2001-03-22 15:07:49 Re: OID as Primary Key