pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Complex Update
2+ messages / 2 participants
[nested] [flat]

* Complex Update
@ 2004-01-03 09:20 John Coryat <john@coryat.com>
  2004-01-05 11:10 ` Re: Complex Update Richard Huxton <dev@archonet.com>
  0 siblings, 1 reply; 2+ messages in thread

From: John Coryat @ 2004-01-03 09:20 UTC (permalink / raw)
  To: pgsql-sql

Problem: I have a table that has a field I want to plug with a value 
from other tables.

I can do it with a perl program and a loop, but want to do it with a 
single SQL statement.

Is there a way to do an update that combines a select statement 
containing a "where" that uses a field from the row to be updated?

I would expect it to look something like this:

update orderheader set total = (select sum(amount) from orderdetail 
where orderdetail.orderno=orderheader.orderno) ;

Any ideas?




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Complex Update
  2004-01-03 09:20 Complex Update John Coryat <john@coryat.com>
@ 2004-01-05 11:10 ` Richard Huxton <dev@archonet.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Richard Huxton @ 2004-01-05 11:10 UTC (permalink / raw)
  To: John Coryat <john@coryat.com>; pgsql-sql

On Saturday 03 January 2004 09:20, John Coryat wrote:
> Problem: I have a table that has a field I want to plug with a value
> from other tables.
[snip]
> I would expect it to look something like this:
>
> update orderheader set total = (select sum(amount) from orderdetail
> where orderdetail.orderno=orderheader.orderno) ;

Looks fine to me. In general, you can do something of the form:

UPDATE ta SET a2 = (SELECT expr(b2) FROM tb WHERE b1=a1);

NOTE - make sure you understand what happens when there is no b1=a1 and what 
that will mean to your particular setup.

-- 
  Richard Huxton
  Archonet Ltd



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-01-05 11:10 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-01-03 09:20 Complex Update John Coryat <john@coryat.com>
2004-01-05 11:10 ` Richard Huxton <dev@archonet.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox