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

* Update from same table
@ 2004-02-04 16:46  Jürgen Cappel <email@juergen-cappel.de>
  0 siblings, 1 reply; 2+ messages in thread

From: Jürgen Cappel @ 2004-02-04 16:46 UTC (permalink / raw)
  To: pgsql-sql

Hello,

I want to update columns in a table that match a fixed key
from the same column of the same table matching another fixed key.
There can be several tuples per key distinguished by a secondary id.
Tuples are unique with the combined keys. Maybe a query could look
something like this:

	UPDATE a.mytable from b.mytable
	SET a.mycolumn = b.mycolumn
	WHERE a.firstid = some_key
	AND b.firstid = some_other_key
	AND a.secondaryid = b.secondaryid;

But it's a syntax error. I tried a subselect but i'm failing to connect
the subselect's 2nd ID with the update's 2nd ID.  Any help ?  Thanks,

Jürgen




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

* Re: Update from same table
@ 2004-02-04 17:28  Josh Berkus <josh@agliodbs.com>
  parent: Jürgen Cappel <email@juergen-cappel.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Josh Berkus @ 2004-02-04 17:28 UTC (permalink / raw)
  To: Jürgen Cappel <email@juergen-cappel.de>; pgsql-sql

Jurgen,

> 	UPDATE a.mytable from b.mytable
> 	SET a.mycolumn = b.mycolumn
> 	WHERE a.firstid = some_key
> 	AND b.firstid = some_other_key
> 	AND a.secondaryid = b.secondaryid;

Very close, actually; you just need to fix the table alias:

 	UPDATE mytable 
	FROM mytable as b
 	SET mytable.mycolumn = b.mycolumn
	WHERE mytable.firstid = some_key
 	AND b.firstid = some_other_key
 	AND mytable.secondaryid = b.secondaryid;

AFAIK, one can't alias a table name in an UPDATE clause.  So for that instance 
of the table, you need to use the full name.
 

-- 
-Josh Berkus
 Aglio Database Solutions
 San Francisco




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


end of thread, other threads:[~2004-02-04 17:28 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-02-04 16:46 Update from same table Jürgen Cappel <email@juergen-cappel.de>
2004-02-04 17:28 ` Josh Berkus <josh@agliodbs.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