pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <craig@postnewspapers.com.au>
To: johnf <jfabiani@yolo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: using a list to query
Date: Sun, 03 May 2009 09:34:57 +0800
Message-ID: <49FCF4C1.8020701@postnewspapers.com.au> (raw)
In-Reply-To: <200905020955.29386.jfabiani@yolo.com>
References: <200905020955.29386.jfabiani@yolo.com>

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



view thread (3+ messages)  latest in thread

Message-ID: <49FCF4C1.8020701@postnewspapers.com.au>
Permalink:  ../49FCF4C1.8020701@postnewspapers.com.au/
Also on:    postgresql.org/message-id/49FCF4C1.8020701@postnewspapers.com.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: craig@postnewspapers.com.au, jfabiani@yolo.com
  Subject: Re: using a list to query
  In-Reply-To: <49FCF4C1.8020701@postnewspapers.com.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox