Search In Integer Array In Postgres
Is there any other way to search for a certain value in an integer[] column in Postgres? My currently installed Postgres version does not allow the following statement: SELECT * FR
Solution 1:
For equality checks you can simply:
SELECT*FROMtableWHERE10=ANY (values);
Read about ANY/SOME in the manual.
See also:
Solution 2:
quickly search will be so, but you should use index gist or gin for intarray type Postgres intarray
SELECT*FROMtableWHEREvalues @>ARRAY[10];
Solution 3:
**Store IntegerArrayas Strings in Postgresql and Query the Array**
Finally I could save the integeras string arrayinonecolumn able to successfully convertintoarrayand query the arrayusing below example.
CREATETABLE test
(
yearcharactervarying,
id serial NOTNULL,
category_id charactervarying,
CONSTRAINT test_pkey PRIMARY KEY (id)
)
Data
"2005";1;"1,2,3,4"
"2006";2;"2,3,5,6"
"2006";3;"4,3,5,6"
"2007";7;"1,2"
selectdistinct(id) from test, (select id as cid, unnest(string_to_array(category_id , ',')::integer[]) as cat from test) c where c.cid=test.id and cat in (1,2,3);
Result:
2137
Post a Comment for "Search In Integer Array In Postgres"