pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: johnf <jfabiani@yolo.com>
To: pgsql-sql@postgresql.org
Subject: Re: using a list to query
Date: Sun, 3 May 2009 15:26:58 -0700
Message-ID: <200905031526.58125.jfabiani@yolo.com> (raw)
In-Reply-To: <49FCF4C1.8020701@postnewspapers.com.au>
References: <200905020955.29386.jfabiani@yolo.com>
	<49FCF4C1.8020701@postnewspapers.com.au>

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



view thread (3+ messages)

Message-ID: <200905031526.58125.jfabiani@yolo.com>
Permalink:  ../200905031526.58125.jfabiani@yolo.com/
Also on:    postgresql.org/message-id/200905031526.58125.jfabiani@yolo.com

 · 

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: jfabiani@yolo.com
  Subject: Re: using a list to query
  In-Reply-To: <200905031526.58125.jfabiani@yolo.com>

* 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