From: | Merlin Moncure <mmoncure(at)gmail(dot)com> |
---|---|
To: | Nick <nboutelier(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Regular expression and array |
Date: | 2009-05-27 13:58:04 |
Message-ID: | b42b73150905270658i1d03f8d5m88414723e0a7e21b@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Tue, May 26, 2009 at 11:04 PM, Nick <nboutelier(at)gmail(dot)com> wrote:
> I wont go into details about why im using this field as an array but
> how would I select all the rows that have the first name 'Tom' out of
> the 'names' field?
>
> CREATE TABLE test (
> id integer,
> names character varying[]
> );
> INSERT INTO test VALUES (1, '{"''Josh Berkus''","''Peter
> Eisentraut''","''Marc Fournier''"}');
> INSERT INTO test VALUES (2, '{"''Tom Lane''","''Bruce
> Momjian''","''Dave Page''"}');
> INSERT INTO test VALUES (3, '{"''Jan Wieck''","''Oleg
> Bartunov''","''Joe Conway''"}');
couple of ways:
select * from test where 'Jan Wieck' = any(names);
on 8.4:
select * from (select unnest(names) as n from test) q where n = 'Jan Wieck';
while the second approach seems more complex, it's a more general type
of thing that can be used to attack all kinds of problems. Previous
to 8.4 you have to write your own unnest (it's not hard) or use the
built in information_schema._pg_expandarray();
merlin
From | Date | Subject | |
---|---|---|---|
Next Message | Merlin Moncure | 2009-05-27 14:02:36 | Re: composite type and domain |
Previous Message | Alexander Schöcke | 2009-05-27 13:51:14 | Bloated Table |