Re: ...where 'value' = array[]

Lists: pgsql-general
From: zach cruise <zachc1980(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: ...where 'value' = array[]
Date: 2009-04-16 22:15:20
Message-ID: bcdac0e80904161515g1b45177hd7c246a1313beddd@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

i have table, like so:
group.group_name (varchar) | group.group_array (varchar[])
-
west coast | {CA,WA}
east coast | {NY,MA}

i can do this:
select group_name from group where 'CA' = any(array['CA','WA']);

but i need to select group_name where state_abbreviation is in
group_array, something like:
select group_name from group where 'CA' = any(group_array);
or
select group_name from group where 'CA' in (group_array);
or
select group_name from group where 'CA' in (select group_array from
group where true);

all ()[]{} have me confused
http://www.nabble.com/IN-with-arrays-td10011058.html


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: zach cruise <zachc1980(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ...where 'value' = array[]
Date: 2009-04-16 22:32:35
Message-ID: 6488.1239921155@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

zach cruise <zachc1980(at)gmail(dot)com> writes:
> but i need to select group_name where state_abbreviation is in
> group_array, something like:
> select group_name from group where 'CA' = any(group_array);

Yeah? What's the problem?

regression=# create table g (group_name text, group_array text[]);
CREATE TABLE
regression=# insert into g values ('west', '{CA,WA}');
INSERT 0 1
regression=# insert into g values ('east', '{NY,MA}');
INSERT 0 1
regression=# select * from g where 'CA' = any(group_array);
group_name | group_array
------------+-------------
west | {CA,WA}
(1 row)

This has worked since 7.4 or so ...

regards, tom lane