pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Pundt <mlists@rp-online.de>
To: pgsql-sql@postgresql.org, "Joost Kraaijeveld" <J.Kraaijeveld@askesis.nl>
Subject: Re: How to add column from old_table to new_table?
Date: Wed, 21 Sep 2005 14:58:15 +0200
Message-ID: <200509211458.15281.mlists@rp-online.de> (raw)
In-Reply-To: <1127306186.20038.21.camel@Panoramix>
References: <1127306186.20038.21.camel@Panoramix>

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.pundt@rp-online.de> ---- http://rp-online.de/ ----




view thread (9+ messages)  latest in thread

Message-ID: <200509211458.15281.mlists@rp-online.de>
Permalink:  ../200509211458.15281.mlists@rp-online.de/
Also on:    postgresql.org/message-id/200509211458.15281.mlists@rp-online.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mlists@rp-online.de, J.Kraaijeveld@askesis.nl
  Subject: Re: How to add column from old_table to new_table?
  In-Reply-To: <200509211458.15281.mlists@rp-online.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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