pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
using a list to query
3+ messages / 2 participants
[nested] [flat]

* using a list to query
@ 2009-05-02 16:55  johnf <jfabiani@yolo.com>
  0 siblings, 1 reply; 3+ messages in thread

From: johnf @ 2009-05-02 16:55 UTC (permalink / raw)
  To: pgsql-sql

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?
-- 
John Fabiani



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: using a list to query
@ 2009-05-03 01:34  Craig Ringer <craig@postnewspapers.com.au>
  parent: johnf <jfabiani@yolo.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Craig Ringer @ 2009-05-03 01:34 UTC (permalink / raw)
  To: johnf <jfabiani@yolo.com>; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: using a list to query
@ 2009-05-03 22:26  johnf <jfabiani@yolo.com>
  parent: Craig Ringer <craig@postnewspapers.com.au>
  0 siblings, 0 replies; 3+ messages in thread

From: johnf @ 2009-05-03 22:26 UTC (permalink / raw)
  To: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2009-05-03 22:26 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-05-02 16:55 using a list to query johnf <jfabiani@yolo.com>
2009-05-03 01:34 ` Craig Ringer <craig@postnewspapers.com.au>
2009-05-03 22:26   ` johnf <jfabiani@yolo.com>

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