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
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
;