Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在postgresql数据库中使用JSON文件_Json_Postgresql - Fatal编程技术网

在postgresql数据库中使用JSON文件

在postgresql数据库中使用JSON文件,json,postgresql,Json,Postgresql,我在postgresql数据库中有一个表,其中有一个json格式的字段。 看起来是这样的: [ { "transition": "transition1", "from": "step1", "to": "step2", "date": { "date": "2021-01-30

我在postgresql数据库中有一个表,其中有一个json格式的字段。 看起来是这样的:

[
  {
    "transition": "transition1",
    "from": "step1",
    "to": "step2",
    "date": {
      "date": "2021-01-30 15:34:06.840859"
    }
  },
  {
    "transition": "transition2",
    "from": "step2",
    "to": "step3",
    "date": {
      "date": "2021-01-30 16:52:00.412208"
    }
  }
]
我想有一个新的列,上面写着转换日期1。
我尝试了很多方法,但我不知道如何提取此日期,我无法使用索引,因为转换的数量不是固定的,有些用户可能有3个,其他的可能超过10个。

您需要迭代所有数组元素以找到正确的一个:

select (select (item -> 'date' ->> 'date')::timestamp
        from jsonb_array_elements(jsonb_column) as x(item)
        where item ->> 'transition' = 'transition1') as transition_date
from the_table
;

如果列定义为
json
,而不是
jsonb
(应该是这样),则需要使用
json\u array\u elements()

需要迭代所有数组元素以找到正确的元素:

select (select (item -> 'date' ->> 'date')::timestamp
        from jsonb_array_elements(jsonb_column) as x(item)
        where item ->> 'transition' = 'transition1') as transition_date
from the_table
;

如果将列定义为
json
,而不是
jsonb
(应该是这样),则需要使用
json\u array\u elements()
相反。

你的Postgres版本是什么?你的Postgres版本是什么?我的版本是:11.6我可以用同样的东西不更新表格而选择这个值吗?@J.doe:是的,你可以把这个表达式放进一个SELECTMy version is:11.6我可以用同样的东西不更新表格而选择这个值吗?@J.doe:是的,你可以放进去表达式转换为SELECT