[英]How to append a custom object to JSONB column type in postgres
I need to update a jsonb column which contains data {"X":true}
.我需要更新一个包含数据
{"X":true}
的 jsonb 列。 I need to append a complex object of the type {"obj":{"a":1,"b":2}}
so the final value of row for that column {"x":true,"obj":{"a":1,"b":2}}
.我需要 append 一个复杂的 object 类型
{"obj":{"a":1,"b":2}}
所以该列的行的最终值{"x":true,"obj":{"a":1,"b":2}}
。 What will be the query to update this row.更新此行的查询将是什么。
postgres version 12 postgres 版本 12
Update - The following query update tableName set columnName = (select '{"obj":{"a":1,"b":2}}'::jsonb || columnName::jsonb) where...
returns successfully when there is a value present, but when the column is null it still remains null after running the update query.更新 - 以下查询
update tableName set columnName = (select '{"obj":{"a":1,"b":2}}'::jsonb || columnName::jsonb) where...
返回成功时存在一个值,但是当列是 null 时,它在运行更新查询后仍然是 null。 I need to be able to add {"obj":{"a":1,"b":2}}
even when the column is null.即使列是 null,我也需要能够添加
{"obj":{"a":1,"b":2}}
。
You can use the concatenation operator :您可以使用连接运算符:
'{"X":true}'::jsonb || '{"obj":{"a":1,"b":2}}'::jsonb
If you want to update an existing column, use coalesce()
to deal with NULL values:如果要更新现有列,请使用
coalesce()
处理 NULL 值:
update the_table
set the_column = coalesce(the_column, '{}')||'{"obj":{"a":1,"b":2}}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.