X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id C63F6D1B467 for ; Wed, 10 Mar 2004 20:56:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 58562-08 for ; Wed, 10 Mar 2004 16:56:46 -0400 (AST) Received: from outputservices.com (outputt1130.customer.frii.net [216.17.159.130]) by svr1.postgresql.org (Postfix) with ESMTP id 87D22D1B461 for ; Wed, 10 Mar 2004 16:56:43 -0400 (AST) Received: from outputservices.com (outputservices.com [137.106.76.15]) by outputservices.com (8.11.3/8.11.3) with ESMTP id i2AKuki26782 for ; Wed, 10 Mar 2004 13:56:46 -0700 (MST) Message-ID: <404F810E.1010707@outputservices.com> Date: Wed, 10 Mar 2004 13:56:46 -0700 From: Marty Scholes User-Agent: Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.0.1) Gecko/20020920 Netscape/7.0 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql Subject: Re: Inserting data in a table using sub-selects] Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/118 X-Sequence-Number: 17217 INSERT INTO table2 (id, content) ( SELECT id, coalesce(title, '') || ' ' || coalesce(description, '') FROM table1 t1); If you want to keep them syncrhonized, in other words, rerun the query over and over again without having to truncate table2 first or deleting all of the rows, you can: INSERT INTO table2 (id, content) ( SELECT id, coalesce(title, '') || ' ' || coalesce(description, '') FROM table1 t1) WHERE id NOT IN ( SELECT id FROM table1); With Oracle there is a slick way to do a partial outer join that allowed you to do this without creating a complete list of table1.id in the last subquery, but I dunno if Pg has an equivalent mechanism. 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); > > Any hints on how to insert a lot of values at the same time like this? > > - -- > Andreas Joseph Krogh > Managing Director, Senior Software Developer > OfficeNet AS > > I always do a CVS update before making a patch (unless I forget). > > gpg public_key: http://dev.officenet.no/~andreak/public_key.asc > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.2 (GNU/Linux) > > iD8DBQFAT3H1UopImDh2gfQRAsa6AJ9jZjNz25w4iVnxNJYY9LJuG0HBLACfZfup > 1TMzQSi1+YYgNjpcampX6wo= > =fJA3 > -----END PGP SIGNATURE----- > > ---------------------------(end of broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings