Search in integer array in Postgres

前端 未结 3 1441
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 04:28

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

3条回答
  •  我寻月下人不归
    2021-02-04 05:02

    **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
    

提交回复
热议问题