Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 105B16325D1 for ; Sun, 3 May 2009 19:27:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 96961-01 for ; Sun, 3 May 2009 19:26:55 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wavecable.net (mail3.wavecable.com [24.113.32.25]) by mail.postgresql.org (Postfix) with ESMTP id A00C16323F0 for ; Sun, 3 May 2009 19:27:01 -0300 (ADT) Received: from [76.14.161.10] (HELO [192.168.1.215]) by wavecable.net (CommuniGate Pro SMTP 4.2.8) with ESMTP id 152661049 for pgsql-sql@postgresql.org; Sun, 03 May 2009 15:17:51 -0700 From: johnf To: pgsql-sql@postgresql.org Subject: Re: using a list to query Date: Sun, 3 May 2009 15:26:58 -0700 User-Agent: KMail/1.9.6 (enterprise 20070904.708012) References: <200905020955.29386.jfabiani@yolo.com> <49FCF4C1.8020701@postnewspapers.com.au> In-Reply-To: <49FCF4C1.8020701@postnewspapers.com.au> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200905031526.58125.jfabiani@yolo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200905/12 X-Sequence-Number: 32504 On Saturday 02 May 2009 06:34:57 pm Craig Ringer wrote: > johnf wrote: > > I have a list (or array) of primary keys stored in a field (data type > > text). I would like to use the list to retrieve all the data from a table > > based on the list. > > > > my text field contains: > > '123,134,343,345' > > > > I would to do something like the following: > > > > Select * from table1 where table1.pkid in (select myTextfield from > > table2) > > > > So the question is there a simple way to use the list to retrieve my > > data? > > http://www.postgresql.org/docs/8.3/static/functions-matching.html > > SELECT * FROM table1 > WHERE table1.pkid IN ( > SELECT x FROM regexp_split_to_table(myTextfield, ',') AS x > ); > > ... but you should consider storing your list in an array instead, or > using a more conventional child table with a (pkid, refid) pair list. > > -- > Craig Ringer Thanks - I think this will work very well. I considered an array but at the moment I don't have an easy way of retrieving data from an array. I'm working on that as I type. The other solution would be a table but that seems over kill for one field. Thanks again -- John Fabiani