postgresql中的jsonb更新操作,update操作

postgresql中更新jsonb中某一个子属性的值需要将其转换为jsonb格式,如果不转换只能是string类型,不能是其他类型

 

detail_info 中的子元素子属性增加固定值

detail_info 元素为 :{"student": {"educationStats": {"other": 0, "doctor": 0, "master": 0, "college": 0, "bachelor": 0}}}

需要 to_jsonb()做转换

update
    t_job_info
set
    detail_info = jsonb_set(detail_info, '{student,educationStats,other}', to_jsonb((((detail_info::json#>>'{student}')::json#>>'{educationStats}')::json#>>'{other}')::text::int + 1), false)
where
    id = 80;

要注意的一点是,需要将jsonb_set()中第三个属性,也就是newValue 用 to_jsonb()做一次转换,如果newValue是string类型则不用修改

例如:不用to_jsonb()做转换

update
    t_job_info
set
    detail_info = jsonb_set(detail_info, '{student,educationStats,other}', '23', false)
where
    id = 80;

花了2小时查阅文档发现并没有明确说明,自己摸索得出,感谢支持

翻阅资料:http://postgres.cn/docs/9.6/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值