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

Re: [GENERAL] Turning a subselect into an array

From: "Jim C(dot) Nasby" <decibel(at)decibel(dot)org>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-docs(at)postgresql(dot)org
Subject: Re: [GENERAL] Turning a subselect into an array
Date: 2004-10-30 06:07:37
Message-ID: 20041030060737.GB55164@decibel.org (view raw or flat)
Thread:
Lists: pgsql-docspgsql-general
Thanks again for the help. I did manage to find that after a bunch of
searching in the mailing list. There should really be a portion of the
docs dedicated to array handling.

On Fri, Oct 29, 2004 at 10:40:57PM -0600, Michael Fuhr wrote:
> On Fri, Oct 29, 2004 at 05:13:02PM -0500, Jim C. Nasby wrote:
> > Ok, next stupid question that I can't find in the docs... How would I
> > join a table to an array? IE: if I have an array of primary keys for
> > some table and I want to get the name field from that table and turn it
> > back into an array, how would I do that?
> 
> See the "Subquery Expressions" and "Row and Array Comparisons"
> sections in the PostgreSQL documentation:
> 
> http://www.postgresql.org/docs/7.4/static/functions-subquery.html
> http://www.postgresql.org/docs/7.4/static/functions-comparisons.html
> 
> If I understand you correctly, this should work:
> 
> SELECT ARRAY(SELECT name FROM foo WHERE id = ANY(ARRAY[1,2,3]));
> 
> A test I just ran showed "id = ANY(ARRAY[1,2,3])" doing a sequential
> scan whereas "id IN (1,2,3)" did an index scan, so you might want to
> use the latter if possible.
> 
> -- 
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
> 

-- 
Jim C. Nasby, Database Consultant               decibel(at)decibel(dot)org 
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

In response to

pgsql-docs by date

Next:From: Josh BerkusDate: 2004-11-01 00:16:45
Subject: Translators wanted for 8.0 Release
Previous:From: Michael FuhrDate: 2004-10-30 04:40:57
Subject: Re: Turning a subselect into an array

pgsql-general by date

Next:From: Russell SmithDate: 2004-10-30 06:09:21
Subject: Re: adding missing FROM-clause
Previous:From: Russell SmithDate: 2004-10-30 06:02:34
Subject: Re: QMail

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