convert jsonb column value to multiple columns in PostgreSQL

后端 未结 1 1846
轮回少年
轮回少年 2021-01-14 11:30

lets say i have a table in PostgreSQL with the following columns:

CREATE TABLE sample
(
id int,
jsonb jsonb,
date date
)

and i inserted thi

相关标签:
1条回答
  • 2021-01-14 12:07

    It is simple in 9.4 (used LATERAL join and jsonb functions):

        postgres=# SELECT * 
                      FROM sample, jsonb_to_record(jsonb, true) AS x(a text, b text);
         id |            jsonb             |    date     |  a   |   b    
        ----+------------------------------+-------------+------+--------
          1 | {"a": "a", "b": "b"}         | 2014-01-06  | a    | b
          2 | {"a": "a", "b": "b"}         | 2014-01-06  | a    | b
          3 | {"a": "Ahoj", "b": "Nazdar"} | 2014-01-06  | Ahoj | Nazdar
        (3 rows)
    

    exact result:

    postgres=# SELECT id, a, b, date 
                   FROM sample, jsonb_to_record(jsonb, true) AS x(a text, b text);
     id |  a   |   b    |    date    
    ----+------+--------+------------
      1 | a    | b      | 2014-01-06
      2 | a    | b      | 2014-01-06
      3 | Ahoj | Nazdar | 2014-01-06
    (3 rows)
    
    0 讨论(0)
提交回复
热议问题