2011-11-23 42 views
7

在Postgres的integer[]列中有沒有其他方法可以搜索某個值?在Postgres的整數數組中搜索

我目前安裝的Postgres版本則允許以下語句:

SELECT * FROM table WHERE values *= 10; 

陣列的例子:

'{11043,10859,10860,10710,10860,10877,10895,11251}' 
'{11311,10698,10697,10710,10712,10711,10708}' 

聲明,其中數組包含'10710'應該返回每一行。

回答

18

對於平等檢查,你可以簡單地說:

SELECT * FROM table WHERE 10 = ANY (values); 

閱讀ANY/SOME in the manual

+0

梅爾切,DES WOS。 :) – jussi

1

快速搜索會是這樣,但你應該使用索引要點或杜松子酒的intarray型Postgres intarray

SELECT * FROM table WHERE values @> ARRAY[10]; 
0
**Store Integer Array as Strings in Postgresql and Query the Array**  
Finally I could save the integer as string array in one column able to successfully convert into array and query the array using below example. 

    CREATE TABLE test 
    (
     year character varying, 
     id serial NOT NULL, 
     category_id character varying, 
     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" 


    select distinct(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: 
    2 
    1 
    3 
    7