Skip site navigation (1) Skip section navigation (2)

Re: Inserting data in a table using sub-selects

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Andreas Joseph Krogh <andreak(at)officenet(dot)no>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Inserting data in a table using sub-selects
Date: 2004-03-10 20:49:34
Message-ID: 20040310124834.X36099@megazone.bigpanda.com (view raw or flat)
Thread:
Lists: pgsql-sql
On Wed, 10 Mar 2004, Andreas Joseph Krogh wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi, I'd like to fill one table with the contents of another table. Mye schema
> is like this:
>
> CREATE TABLE table1(
> id serial NOT NULL PRIMARY KEY,
> title varchar NOT NULL,
> description varchar
> );
>
> CREATE TABLE table2(
> id int NOT NULL REFERENCES(table1(id) ON DELETE CASCADE,
> content varchar NOT NULL
> );
>
> Now - is there a way I can do something like:
> INSERT INTO table2(id, content) values (select t1.id, coalesce(t1.title, '')
> || ' ' || coalesce(t1.description, '') as content from table1 t1);

I think you mostly just want to remove the values():

Insert into table2(id, content) select t1.id, coalesce(t1.title, '')
 || ' ' || coalesce(t1.description, '') as content from table t1;

In response to

pgsql-sql by date

Next:From: Marty ScholesDate: 2004-03-10 20:56:46
Subject: Re: Inserting data in a table using sub-selects]
Previous:From: Rajesh Kumar MallahDate: 2004-03-10 19:54:17
Subject: Tsearch2 question: getting histogram of the vector elements

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group