unwrap postgresql array into rows

后端 未结 2 1476
梦毁少年i
梦毁少年i 2020-12-13 23:28

What is the fastest way to unwrap array into rows in PostgreSQL? For instance,

We have:

a
-
{1,2}
{2,3,4}

And we need:



        
相关标签:
2条回答
  • 2020-12-13 23:48

    unnest --> expand an array to a set of rows

    unnest(ARRAY[1,2]) 1 2

    http://www.sqlfiddle.com/#!1/c774a/24

    0 讨论(0)
  • 2020-12-14 00:01

    Use unnest. For example:

    CREATE OR REPLACE FUNCTION test( p_test text[] )
      RETURNS void AS
    $BODY$
    BEGIN
      SELECT id FROM unnest( p_test ) AS id;
    END;
    $BODY$
      LANGUAGE plpgsql IMMUTABLE
      COST 1;
    
    0 讨论(0)
提交回复
热议问题