簡體   English   中英

在Postgres中搜索整數數組

[英]Search in integer array in Postgres

有沒有其他方法可以在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'每一行。

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

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

閱讀手冊中的ANY / SOME

快速搜索將如此,但你應該使用索引gist或杜松子酒intarray類型Postgres intarray

 SELECT * FROM table WHERE values @> ARRAY[10];
**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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM