jsonb字段中的PostgreSQL重命名属性 [英] PostgreSQL rename attribute in jsonb field
本文介绍了jsonb字段中的PostgreSQL重命名属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PostgreSQL 9.5中,是否可以重命名jsonb字段中的属性?
In postgresql 9.5, is there a way to rename an attribute in a jsonb field?
例如:
{ "nme" : "test" }
应重命名为
{ "name" : "test"}
推荐答案
在UPDATE
中,使用删除(-)和并置(||)运算符,例如:
create table example(id int primary key, js jsonb);
insert into example values
(1, '{"nme": "test"}'),
(2, '{"nme": "second test"}');
update example
set js = js - 'nme' || jsonb_build_object('name', js->'nme')
where js ? 'nme'
returning *;
id | js
----+-------------------------
1 | {"name": "test"}
2 | {"name": "second test"}
(2 rows)
这篇关于jsonb字段中的PostgreSQL重命名属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文