Remove array values in pgSQL

后端 未结 10 2219
不知归路
不知归路 2021-02-06 22:10

Is there a way to remove a value from an array in pgSQL? Or to be more precise, to pop the last value? Judging by this list the answer seems to be no. I can get the result I wan

相关标签:
10条回答
  • 2021-02-06 22:43

    Here is a function I use for integer[] arrays

    CREATE OR REPLACE FUNCTION array_remove_item (array_in INTEGER[], item INTEGER)
    RETURNS INTEGER[]
    LANGUAGE SQL
    AS $$
    SELECT ARRAY(
      SELECT DISTINCT $1[s.i] AS "foo"
        FROM GENERATE_SERIES(ARRAY_LOWER($1,1), ARRAY_UPPER($1,1)) AS s(i)
       WHERE $2 != $1[s.i]
       ORDER BY foo
    );
    $$;
    

    This is obviously for integer arrays but could be modified for ANYARRAY ANYELEMENT

    => select array_remove_item(array[1,2,3,4,5], 3);
    -[ RECORD 1 ]-----+----------
    array_remove_item | {1,2,4,5}
    
    0 讨论(0)
  • 2021-02-06 22:47

    I'm not sure about your context, but this should give you something to work with:

    CREATE TABLE test (x INT[]);
    INSERT INTO test VALUES ('{1,2,3,4,5}');
    
    SELECT x AS array_pre_pop,
           x[array_lower(x,1) : array_upper(x,1)-1] AS array_post_pop, 
           x[array_upper(x,1)] AS popped_value 
    FROM test;
    
    
     array_pre_pop | array_post_pop | popped_value 
    ---------------+----------------+--------------
     {1,2,3,4,5}   | {1,2,3,4}      |            5
    
    0 讨论(0)
  • 2021-02-06 22:48

    My function for all types of arrays.

    Outer function:

    CREATE OR REPLACE FUNCTION "outer_array"(anyarray, anyarray) RETURNS anyarray AS $$
        SELECT
            "new"."item"
        FROM (
            SELECT
                ARRAY(
                    SELECT
                        "arr"."value"
                    FROM (
                        SELECT
                            generate_series(1, array_length($1, 1)) AS "i",
                            unnest($1)                              AS "value"
                    ) "arr"
                    WHERE
                        "arr"."value" <> ALL ($2)
                    ORDER BY
                        "arr"."i"
                ) AS "item"
        ) "new"
        $$
    LANGUAGE sql
    IMMUTABLE
    RETURNS NULL ON NULL INPUT
    ;
    

    Inner function:

    CREATE OR REPLACE FUNCTION "inner_array"(anyarray, anyarray) RETURNS anyarray AS $$
        SELECT
            "new"."item"
        FROM (
            SELECT
                ARRAY(
                    SELECT
                        "arr"."value"
                    FROM (
                        SELECT
                            generate_series(1, array_length($1, 1)) AS "i",
                            unnest($1)                              AS "value"
                    ) "arr"
                    WHERE
                        "arr"."value" = ANY ($2)
                    ORDER BY
                        "arr"."i"
                ) AS "item"
        ) "new"
    $$
    LANGUAGE sql
    IMMUTABLE
    RETURNS NULL ON NULL INPUT
    ;
    
    0 讨论(0)
  • 2021-02-06 22:49

    Try this:

    update table_name set column_name=column_name[1:array_upper(column_name, 1)-1];
    
    0 讨论(0)
提交回复
热议问题