X-Original-To: pgsql-docs-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C13103A3D3A; Sat, 30 Oct 2004 07:07:40 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 99777-02; Sat, 30 Oct 2004 06:07:38 +0000 (GMT) Received: from flake.decibel.org (flake.decibel.org [66.143.173.58]) by svr1.postgresql.org (Postfix) with ESMTP id F00C33A3D31; Sat, 30 Oct 2004 07:07:38 +0100 (BST) Received: by flake.decibel.org (Postfix, from userid 1001) id 1D88C1C902; Sat, 30 Oct 2004 06:07:37 +0000 (GMT) Date: Sat, 30 Oct 2004 01:07:37 -0500 From: "Jim C. Nasby" To: Michael Fuhr Cc: pgsql-general@postgresql.org, pgsql-docs@postgresql.org Subject: Re: [GENERAL] Turning a subselect into an array Message-ID: <20041030060737.GB55164@decibel.org> References: <20041028223729.GF55164@decibel.org> <20041028232152.GA76168@winnie.fuhr.org> <20041029221302.GU55164@decibel.org> <20041030044056.GA19888@winnie.fuhr.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20041030044056.GA19888@winnie.fuhr.org> X-Operating-System: FreeBSD 4.10-RELEASE-p3 i386 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/66 X-Sequence-Number: 2635 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@postgresql.org) > -- Jim C. Nasby, Database Consultant decibel@decibel.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?"