Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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
Sql 如何在postgres中更新JSONB数组_Sql_Arrays_Json_Postgresql - Fatal编程技术网

Sql 如何在postgres中更新JSONB数组

Sql 如何在postgres中更新JSONB数组,sql,arrays,json,postgresql,Sql,Arrays,Json,Postgresql,我有一个定义如下的表: CREATE TABLE USER_CONFIGURATIONS ( ID BIGSERIAL PRIMARY KEY, DATA JSONB ); 我有一个如下所示的数据字段: [ { "user_id": 1, "user_name": "demo_user", "is_manager": 1, "options": [

我有一个定义如下的表:

CREATE TABLE USER_CONFIGURATIONS (
  ID         BIGSERIAL PRIMARY KEY,
  DATA       JSONB
);
我有一个如下所示的数据字段:

[
    {
        "user_id": 1,       
        "user_name": "demo_user", 
        "is_manager": 1,        
        "options": [
            {
                "phone":{
                    "home":"XXXXXXX",
                    "work":"XXXXXXX"
                },
                "address":{
                    "home":"XXXXXXX",
                    "work":"XXXXXXX"
                }
            }
        ]
    },
    ...
]
问题:

  • 如何更新“用户名”
  • 如何更新“选项->电话->主页”

    更新USER\u CONFIGURATIONS SET DATA=jsonb\u SET(…),其中…USER\u id=1

postgres 9.6版本。 我试过使用jsonb_set(),但没有使用wokring

只需运行两次:

update USER_CONFIGURATIONS
set data =  
jsonb_set(
  jsonb_set(
    data,'{0,"user_name"}','"blah"'
  ), '{0,"options",0,"phone","home"}','999999'
)
where id =1
;

只需运行两次:

update USER_CONFIGURATIONS
set data =  
jsonb_set(
  jsonb_set(
    data,'{0,"user_name"}','"blah"'
  ), '{0,"options",0,"phone","home"}','999999'
)
where id =1
;