pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Sullivan <ajs@crankycanuck.ca>
To: pgsql-sql@postgresql.org
Subject: Re: Populating using Select
Date: Tue, 26 Sep 2006 08:52:56 -0400
Message-ID: <20060926125256.GB12878@phlogiston.dyndns.org> (raw)
In-Reply-To: <55c095e90609260535r1951013ep15ec0d5a89c8a78a@mail.gmail.com>
References: <55c095e90609260535r1951013ep15ec0d5a89c8a78a@mail.gmail.com>

On Tue, Sep 26, 2006 at 09:35:18AM -0300, Ezequias Rodrigues da Rocha wrote:
> I was trying to make something like this:
> 
> Insert into base.neigborhood values (nextval(), Select distinct name from
> base.clients).

Not quite.  

INSERT INTO base.neighborhood (column list)
	SELECT nextval('sequence name'),
		name
	FROM	base.clients
	GROUP BY name;

Note that this will give you a different number for each of the
names.  If what you want is for them all to have the same number,
then perform the nextval and then call currval() on the sequence.

By the way, this is a pretty common use of SQL (inserting into one
table the results of a select from another).  If you thought this
wouldn't work, you maybe need a little more SQL practice than you
think you do.  Any of the excellent PostgreSQL books out there should
cover this much.  Alternatively, the standard SQL introductions would
get you there.

A

-- 
Andrew Sullivan  | ajs@crankycanuck.ca
In the future this spectacle of the middle classes shocking the avant-
garde will probably become the textbook definition of Postmodernism. 
                --Brad Holland



view thread (3+ messages)  latest in thread

Message-ID: <20060926125256.GB12878@phlogiston.dyndns.org>
Permalink:  ../20060926125256.GB12878@phlogiston.dyndns.org/
Also on:    postgresql.org/message-id/20060926125256.GB12878@phlogiston.dyndns.org

 · 

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: ajs@crankycanuck.ca
  Subject: Re: Populating using Select
  In-Reply-To: <20060926125256.GB12878@phlogiston.dyndns.org>

* 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