X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BCCE13A3CF5 for ; Sat, 30 Oct 2004 05:41:15 +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 81065-03 for ; Sat, 30 Oct 2004 04:41:06 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id C7BAA3A3CEF for ; Sat, 30 Oct 2004 05:41:05 +0100 (BST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id i9U4ev6F006052 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 29 Oct 2004 22:41:01 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id i9U4evP6019970; Fri, 29 Oct 2004 22:40:57 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id i9U4evNk019969; Fri, 29 Oct 2004 22:40:57 -0600 (MDT) (envelope-from mfuhr) Date: Fri, 29 Oct 2004 22:40:57 -0600 From: Michael Fuhr To: "Jim C. Nasby" Cc: pgsql-general@postgresql.org Subject: Re: Turning a subselect into an array Message-ID: <20041030044056.GA19888@winnie.fuhr.org> References: <20041028223729.GF55164@decibel.org> <20041028232152.GA76168@winnie.fuhr.org> <20041029221302.GU55164@decibel.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20041029221302.GU55164@decibel.org> 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/1480 X-Sequence-Number: 68003 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/