Re: update table where rows are selected by inner join?

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Jeff Kowalczyk <jtk(at)yahoo(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: update table where rows are selected by inner join?
Date: 2004-05-10 22:09:43
Message-ID: 20040510150800.Y21776@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, 10 May 2004, Jeff Kowalczyk wrote:

> I have two tables orders and customerpaymentnote, which keep denormalized
> columns of the status in rows related by orderid. The column duplication
> is intentional, to ease end-user ad-hoc queries. I don't understand the
> UPDATE FROM clause at:
> http://www.postgresql.org/docs/7.4/static/sql-update.html
>
> I have a query working (slowly) in MS Access with the pgodbc driver, but I
> need to know the Postgres version of the SQL.
>
> Working (but slow) MS Access version:
> UPDATE orders
> INNER JOIN customerpaymentnote
> ON orders.orderid=customerpaymentnote.orderid
> SET orders.customerdatetimepaid = customerpaymentnote.customerdatetimepaid,
> orders.customerchargeaspaid = customerpaymentnote.customerchargeaspaid,
> orders.customerchargestatus = "Payment-Recieved",
> orders.orderworkflowbillingstateid = "Payment-Recieved";
>
> Postgres translation, which does not yet work:
> UPDATE orders
> SET customerdatetimepaid = customerpaymentnote.customerdatetimepaid,
> customerchargeaspaid = customerpaymentnote.customerchargeaspaid,
> customerchargestatus = "Payment-Recieved",
> orderworkflowbillingstateid = "Payment-Recieved"
> FROM orders INNER JOIN customerpaymentnote
> ON orders.orderid=customerpaymentnote.orderid

I think you just want something like:
FROM customerpaymentnote
WHERE orders.orderid=customerpaymentnot.orderid;

The update table (orders in this case) is effectively already included in
the "from" list.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jeff Kowalczyk 2004-05-10 22:13:44 update table where rows are selected by inner join?
Previous Message Jeff Eckermann 2004-05-10 21:38:56 Re: Trigger function to know which fields are being updated