如何从Postgresql jsonb列检索数据

时间:2018-10-27 23:32:48

标签: sql json postgresql

我在postgres jsonb列-{"purpose_of_name":"official","surname":"Donald","first_name":"Duck"}中有一个类似此存储的数据。 json元素可以或多或少地更改。我该如何检索数据

purpose_of_name| surname| first_name
----------+----------+--------------------
   official|       Donald |  Duck 

如果还有更多元素

{"purpose_of_name":"official","surname":"Donald","first_name":"Duck", "last_name" :"Treat"}
purpose_of_name| surname| first_name| last_name
----------+----------+--------------------
   official|       Donald |  Duck | Treat

1 个答案:

答案 0 :(得分:0)

假设表名是“用户”,而jsonb列是“设置”,则查询可以写为:

SELECT
  settings->>'purpose_of_name' AS purpose_of_name
  settings->>'surname' AS surname
  settings->>'first_name' AS first_name
  settings->>'last_name' AS last_name
FROM users;