From: | "Mark Dexter" <MDEXTER(at)dexterchaney(dot)com> |
---|---|
To: | <pgsql-docs(at)postgresql(dot)org> |
Cc: | "Richard Huxton" <dev(at)archonet(dot)com> |
Subject: | UPDATE Query Example |
Date: | 2004-10-18 23:19:48 |
Message-ID: | 5E8F9F5B63726C48836757FE673B584E010EE4C2@dcimail.dexterchaney.local |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-docs |
I think it could be helpful to users to have another example in the
section VI. Reference / I. SQL Commands / UPDATE / Examples. It is not
obvious how to do a more complex UPDATE query with a From clause, e.g.,
one using a LEFT OUTER JOIN , as follows:
Use a LEFT OUTER JOIN with FROM clause syntax. Update employees
commission rate to the higher of their current rate plus 2 percent or
the special bonus rate from the bonus_plan table. In this example, some
employees may not have valid bonus_plan rows in the bonus_plan table.
Note that the table being updated (employees) is named twice and joined
to itself in the WHERE clause.
UPDATE employees SET commission_rate =
CASE when bp.commission_rate > employees.commission_rate + .02 then
bp.commission_rate else employees.commission_rate + .02 end
FROM employees e
LEFT OUTER JOIN bonus_plan bp ON
e.bonus_plan = bp.planid
WHERE employees.employeeid = e.employeeid
Thanks. Mark Dexter
From | Date | Subject | |
---|---|---|---|
Next Message | Hicham G. Elmongui | 2004-10-19 14:21:26 | code in docs gives me an error |
Previous Message | Peter Eisentraut | 2004-10-18 14:12:36 | Re: SQL 2003 conformance |