Re: Help on update that subselects other records in table, uses joins

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Jeff Kowalczyk <jtk(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Help on update that subselects other records in table, uses joins
Date: 2003-10-29 17:42:57
Message-ID: 200310290942.57584.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Jeff,

> UPDATE ordercharges INNER JOIN orders ON
> orders.orderid = ordercharges.orderid
> SET orderchargeasbilled =

You may only UPDATE one table at a time, you can't update a JOIN. So when
selecting from another table to filter or calculate your update, the form is:

UPDATE orderchanges
SET orderchargesbilled = {expression}
FROM orders
WHERE orders.orderid = ordercharges.orderid
AND etc.

Second, your value expression for the update is a subselect which includes a
select on the table and field you are updating! This is a recursive loop and
a very bad idea; gods only know what you'll end up with.

I suggest Joe Conway's "SQL for Smarties" or "SQL Queries for Mere Mortals"
from another author.

--
Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Greg Stark 2003-10-29 17:45:35 Re: [SQL] Table versions
Previous Message Stef 2003-10-29 15:36:53 Re: [SQL] Table versions