【问题标题】:How to get JSONB data in additional columns from Postgres如何从 Postgres 获取附加列中的 JSONB 数据
【发布时间】:2018-10-06 17:10:40
【问题描述】:

我有一个表,我想从中获取两个字段,其中一个是 JSONB,其中 JSONB 值放入新列中。

SELECT ID, ATTRIBS from EMPLOYEE;

这会返回如下输出。

   id  |                attribs
------------------------------------------------------------------------------------
 EM001 | {"Education": "C.A.", "Contact No": "6655448822", "Relative Name": "Ganga"}
 EM002 | {"Education": "M.E.", "Contact No": "6542349992", "Relative Name": "Yamuna"}

我希望输出如下

   id  | Education | Contact No | Relative Name
-----------------------------------------------
 EM001 | C.A.      | 6655448822 | Ganga
 EM002 | M.E.      | 6542349992 | Yamuna

有什么建议吗?

【问题讨论】:

    标签: sql json postgresql


    【解决方案1】:

    使用->> 运算符根据键提取值:

    select id, 
           attribs ->> 'Education' as education,
           attribs ->> 'Contact No' as "Contact No",
           attribs ->> 'Relative Name' as "Relative Name"
    from the_table
    

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2020-11-26
      • 1970-01-01
      • 2016-03-09
      • 2018-03-11
      相关资源
      最近更新 更多