Re: Turning a subselect into an array - Mailing list pgsql-general

From Michael Fuhr
Subject Re: Turning a subselect into an array
Date
Msg-id 20041030044056.GA19888@winnie.fuhr.org
Whole thread Raw
In response to Re: Turning a subselect into an array  ("Jim C. Nasby" <decibel@decibel.org>)
Responses Re: Turning a subselect into an array  ("Jim C. Nasby" <decibel@decibel.org>)
List pgsql-general
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/

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: 8.0 Beta 4 denying network connections?
Next
From: Russell Smith
Date:
Subject: Re: QMail