Postgresql - return entire row as array

前端 未结 4 2192
南方客
南方客 2021-02-09 17:32

Is there a way how you can cast the following result to array?

select pg_tables from pg_tables

This will return one column only, however the da

4条回答
  •  滥情空心
    2021-02-09 18:02

    Might be this: http://www.sqlfiddle.com/#!1/d41d8/364

    select translate(string_to_array(x.*::text,',')::text,'()','')::text[] 
    from pg_tables as x
    

    How it works (inside-out), 5 steps:

    1st:

    select x.*::text from pg_tables as x;
    

    Sample Output:

    |                                                            X |
    ----------------------------------------------------------------
    |                    (pg_catalog,pg_statistic,postgres,,t,f,f) |
    |                         (pg_catalog,pg_type,postgres,,t,f,f) |
    

    2nd:

    select string_to_array(x.*::text,',') from pg_tables as x;
    

    Sample Output:

    |                           STRING_TO_ARRAY |
    ---------------------------------------------
    | (pg_catalog,pg_statistic,postgres,,t,f,f) |
    |      (pg_catalog,pg_type,postgres,,t,f,f) |
    

    3rd:

    select string_to_array(x.*::text,',')::text from pg_tables as x;
    

    Sample Output:

    |                               STRING_TO_ARRAY |
    -------------------------------------------------
    | {(pg_catalog,pg_statistic,postgres,"",t,f,f)} |
    |      {(pg_catalog,pg_type,postgres,"",t,f,f)} |
    

    4th:

    select translate( string_to_array(x.*::text,',')::text, '()', '') from pg_tables as x
    

    Sample Output:

    |                                   TRANSLATE |
    -----------------------------------------------
    | {pg_catalog,pg_statistic,postgres,"",t,f,f} |
    |      {pg_catalog,pg_type,postgres,"",t,f,f} |
    

    Finally:

    select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] 
    from pg_tables as x
    

    Sample Output:

    |                               TRANSLATE |
    -------------------------------------------
    | pg_catalog,pg_statistic,postgres,,t,f,f |
    |      pg_catalog,pg_type,postgres,,t,f,f |
    

    Live test: http://www.sqlfiddle.com/#!1/d41d8/373

    To prove that it works:

    with a as 
    (
      select translate( string_to_array(x.*::text,',')::text, '()', '')::text[] as colArray 
      from pg_tables as x
    )
    select row_number() over(), unnest(colArray)
    from a;
    

    Sample output:

    | ROW_NUMBER |                  UNNEST |
    ----------------------------------------
    |          1 |              pg_catalog |
    |          1 |            pg_statistic |
    |          1 |                postgres |
    |          1 |                         |
    |          1 |                       t |
    |          1 |                       f |
    |          1 |                       f |
    |          2 |              pg_catalog |
    |          2 |                 pg_type |
    |          2 |                postgres |
    |          2 |                         |
    |          2 |                       t |
    |          2 |                       f |
    |          2 |                       f |
    

提交回复
热议问题