如何从Postgres的其他列中获取JSONB数据

时间:2018-10-06 09:01:36

标签: sql json postgresql

我有一张表,我想从中获取两个字段,其中一个是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

有人建议我该怎么做吗?

1 个答案:

答案 0 :(得分:1)

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

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