Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 0FDF2632AE5 for ; Sat, 2 May 2009 22:35:09 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 34618-04 for ; Sat, 2 May 2009 22:35:06 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.61.230.242]) by mail.postgresql.org (Postfix) with ESMTP id 194BC63272B for ; Sat, 2 May 2009 22:35:06 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id AB2725C333; Sun, 3 May 2009 09:35:00 +0800 (WST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 913ED1E0437; Sun, 3 May 2009 09:35:00 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id WEGYQ5R7Eokw; Sun, 3 May 2009 09:34:58 +0800 (WST) Received: from [10.1.1.2] (203-59-118-37.dyn.iinet.net.au [203.59.118.37]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTP id 66D501E0436; Sun, 3 May 2009 09:34:58 +0800 (WST) Message-ID: <49FCF4C1.8020701@postnewspapers.com.au> Date: Sun, 03 May 2009 09:34:57 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.21 (X11/20090409) MIME-Version: 1.0 To: johnf Cc: pgsql-sql@postgresql.org Subject: Re: using a list to query References: <200905020955.29386.jfabiani@yolo.com> In-Reply-To: <200905020955.29386.jfabiani@yolo.com> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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/8 X-Sequence-Number: 32500 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