2011-11-23 19 views
7

में पूर्णांक सरणी में खोजें पोस्टग्रेज़ में 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 
संबंधित मुद्दे