2011-11-23 5 views
7

Postgres의 integer[] 열에서 특정 값을 검색하는 다른 방법이 있습니까?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

머스, 데 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