Re: How to add column from old_table to new_table?

From: Thomas Pundt <mlists(at)rp-online(dot)de>
To: pgsql-sql(at)postgresql(dot)org, "Joost Kraaijeveld" <J(dot)Kraaijeveld(at)askesis(dot)nl>
Subject: Re: How to add column from old_table to new_table?
Date: 2005-09-21 12:58:15
Message-ID: 200509211458.15281.mlists@rp-online.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

On Wednesday 21 September 2005 14:36, Joost Kraaijeveld wrote:
| Is that possible in a SQL script (pgadmin or psql console?) and if so,
| what is the syntax in this example?

funny that you mention pgadmin :-)

| insert into new_table(new_attribute)
| value( select old_attribute from old_table, new_table where old_table.id
| = new_table_id)
| where new_table.id = old_table.id

from the pgadmin help system (reference|sql commands|insert):

INSERT INTO table [ ( column [, ...] ) ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query }

and some lines further an example:

This example inserts some rows into table films from a table tmp_films with
the same column layout as films:

INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07';

Or - if I was misunderstanding your question and you simply want to update
already existing rows in "new_table", you might try

UPDATE new_table
SET new_attribute = old_table.old_attribute
FROM old_table
WHERE new_table.id = old_table.id;

Ciao,
Thomas

--
Thomas Pundt <thomas(dot)pundt(at)rp-online(dot)de> ---- http://rp-online.de/ ----

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Gnanavel S 2005-09-21 12:59:11 Re: How to add column from old_table to new_table?
Previous Message A. Kretschmer 2005-09-21 12:56:25 Re: How to add column from old_table to new_table?