Re: using a list to query

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: johnf <jfabiani(at)yolo(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: using a list to query
Date: 2009-05-03 01:34:57
Message-ID: 49FCF4C1.8020701@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Isaac Dover 2009-05-03 03:47:50 Re: Comparing two tables of different database
Previous Message Lucas Brito 2009-05-02 21:25:45 Re: Comparing two tables of different database