Re: Subselects to populate a table

From: Andrew McMillan <andrew(at)catalyst(dot)net(dot)nz>
To: Chris Boget <chris(at)wild(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Subselects to populate a table
Date: 2003-01-05 05:00:49
Message-ID: 1041742848.5110.114.camel@kant.mcmillan.net.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Sat, 2003-01-04 at 02:25, Chris Boget wrote:
> I've read through the docs and I couldn't find many examples of using
> subselects. I could find out and read all about what they were, but not
> many examples. If someone could point to a page that does show this,
> I'd be ever so appreciative!
> Anyways, I'm redoing a table I imported from MySQL (as part of my
> conversion to PG from MySQL) and I'm having problems with this. The
> error I'm getting is that it says you can't select more than one column.
> Why? Is there another way to do what I'm trying? Here is my query:
>
> INSERT INTO "cards_type_temp"
> ( "card_game", "card_name", "card_table", "record_num" )
> VALUES
> ( 'Vtes', ( SELECT "card_name", "card_type", "record_num" FROM "cards_type" ));
>
> Any help or insight would be great!

INSERT INTO "cards_type_temp" ( "card_game", "card_name", "card_table",
"record_num" ) ( SELECT 'Vtes', "card_name", "card_type", "record_num"
FROM "cards_type" );

i.e. You want to move the constant 'vtes' into the sub-select and get
rid of the VALUES( ) around the subselect.

That's probably the most efficient way anyway, but normally there would
be a WHERE ... clause on that subselect so that you don't get all rows
from cards_type inserted. Maybe you want that in this case.

Cheers,
Andrew.
--
---------------------------------------------------------------------
Andrew @ Catalyst .Net.NZ Ltd, PO Box 11-053, Manners St, Wellington
WEB: http://catalyst.net.nz/ PHYS: Level 2, 150-154 Willis St
DDI: +64(4)916-7201 MOB: +64(21)635-694 OFFICE: +64(4)499-2267
Survey for nothing with http://survey.net.nz/
---------------------------------------------------------------------

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message MAASK Group 2003-01-05 13:05:51 use of shared memory
Previous Message Naval Grau 2003-01-04 16:19:41 Re: last mail a bit confusing,sorry