X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id B4CABD1B55C for ; Wed, 8 Oct 2003 22:11:00 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 55394-07 for ; Wed, 8 Oct 2003 19:10:14 -0300 (ADT) Received: from xeon.unixathome.org (bast.unixathome.org [66.11.174.150]) by svr1.postgresql.org (Postfix) with ESMTP id 516E0D1B4FC for ; Wed, 8 Oct 2003 19:10:11 -0300 (ADT) Received: by xeon.unixathome.org (Postfix, from userid 1000) id BCEDB3E4F; Wed, 8 Oct 2003 18:10:44 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by xeon.unixathome.org (Postfix) with ESMTP id B511D3E4E for ; Wed, 8 Oct 2003 18:10:44 -0400 (EDT) Date: Wed, 8 Oct 2003 18:10:44 -0400 (EDT) From: Dan Langille X-X-Sender: dan@xeon.unixathome.org To: pgsql-sql@postgresql.org Subject: UPDATE one table with values from another Message-ID: <20031008180813.X53328@xeon.unixathome.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/97 X-Sequence-Number: 15497 I know there is a simple solution, but I can't remember what it is. :( I have two similar tables. I want to update the fields from one table to contain the values form the other. The two tables are: laptop.freshports.org=# \d commit_log_ports Table "public.commit_log_ports" Column | Type | Modifiers ---------------+----------+----------- commit_log_id | integer | not null port_id | integer | not null needs_refresh | smallint | not null port_version | text | port_revision | text | Indexes: commit_log_ports_pkey primary key btree (commit_log_id, port_id), needs_refresh btree (needs_refresh) Foreign Key constraints: $1 FOREIGN KEY (commit_log_id) REFERENCES commit_log(id) ON UPDATE CASCADE ON DELETE CASCADE, $2 FOREIGN KEY (port_id) REFERENCES ports(id) ON UPDATE CASCADE ON DELETE CASCADE Triggers: commit_log_ports_insert laptop.freshports.org=# \d commit_log_ports_elements Table "public.commit_log_ports_elements" Column | Type | Modifiers ---------------+----------+----------- commit_log_id | integer | not null element_id | integer | not null needs_refresh | smallint | not null port_version | text | port_revision | text | Indexes: commit_log_ports_elements_pkey primary key btree (commit_log_id, element_id) Foreign Key constraints: $1 FOREIGN KEY (commit_log_id) REFERENCES commit_log(id) ON UPDATE CASCADE ON DELETE CASCADE, $2 FOREIGN KEY (element_id) REFERENCES element(id) ON UPDATE CASCADE ON DELETE CASCADE laptop.freshports.org=# I can obtain the values I want with this query: SELECT CLP.* FROM commit_log_ports CLP, ports P, commit_log_ports_elements X WHERE CLP.port_id = P.id AND CLP.commit_log_id = X.commit_log_id AND X.element_id = P.element_id; I started writing the UPDATE and got as far as this before brain fatigue set in: UPDATE commit_log_ports_elements X SET X.needs_refresh = CLP.needs_refresh, X.port_version = CLP.port_version, X.port_revision = CLP.port_revision WHERE X.commit_log_id = commit_log_ports CLP AND X. A clue please? Thank you.