更新对象的jsonb数组中的键值

编程入门 行业动态 更新时间:2024-10-26 20:28:58
本文介绍了更新对象的jsonb数组中的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有下表company和名为log的jsonb列:-

I have the following table company with a jsonb column named log: -

|code | log ----------------------------------------------------------------------------- |K50 | [{"date": "2002-02-06", "type": "Chg Name", "oldvalue": "TEH "}, {"date": "2003-08-26", "type": "Chg Name", "oldvalue": "TEOA "}] |C44 | [{"date": "2003-05-07", "type": "Chg Name", "oldvalue": "CDE "}]

如何修剪oldvalue中的尾随空白?

How to trim the trailing blanks in the oldvalue?

推荐答案

您可以混合使用 jsonb函数和运算符:

You can do it with a mix of jsonb functions and operators:

UPDATE company c SET log = sub.log2 FROM ( SELECT * FROM company c CROSS JOIN LATERAL ( SELECT jsonb_agg(jsonb_set(l, '{oldvalue}', to_jsonb(rtrim(l->>'oldvalue')))) AS log2 FROM jsonb_array_elements(c.log) l ) sub WHERE jsonb_typeof(log) = 'array' -- exclude NULL and non-arrays ) sub WHERE c.code = sub.code -- assuming code is unique AND c.log <> sub.log2; -- only where column actually changed.

SQL提琴.

更多推荐

更新对象的jsonb数组中的键值

本文发布于:2023-10-23 18:59:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521728.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键值   组中   对象   jsonb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!