Sql 如何在postgres中将自定义对象附加到JSONB列类型

Sql 如何在postgres中将自定义对象附加到JSONB列类型,sql,json,postgresql,sql-update,Sql,Json,Postgresql,Sql Update,我需要更新一个jsonb列,其中包含数据{“X”:true}。 我需要附加一个类型为{“obj”:{“a”:1,“b”:2}的复杂对象,因此该列的行的最终值{“x”:true,“obj”:{“a”:1,“b”:2}。更新此行的查询将是什么 博士后第12版 Update-以下查询updatetablename set columnName=(选择“{”obj”:{“a”:1,“b”:2}}::jsonb | | | columnName::jsonb),其中…在存在值时成功返回,但当列为null时

我需要更新一个jsonb列,其中包含数据
{“X”:true}
。 我需要附加一个类型为
{“obj”:{“a”:1,“b”:2}
的复杂对象,因此该列的行的最终值
{“x”:true,“obj”:{“a”:1,“b”:2}
。更新此行的查询将是什么

博士后第12版


Update-以下查询
updatetablename set columnName=(选择“{”obj”:{“a”:1,“b”:2}}::jsonb | | | columnName::jsonb),其中…
在存在值时成功返回,但当列为null时,在运行更新查询后仍然保持null。我需要能够添加
{“obj”:{“a”:1,“b”:2}}
,即使列为空。

您可以使用:

如果要更新现有列,请使用
coalesce()
处理空值:

update the_table
  set the_column = coalesce(the_column, '{}')||'{"obj":{"a":1,"b":2}}'

以下查询
updatetablename set columnName=(选择“{”obj”:{“a”:1,“b”:2}}}::jsonb | | | columnName::jsonb)其中…
在存在值时成功返回,但当列为null时,在运行更新查询后仍保持null。@divyamsingh13:使用
coalesce()
-请参阅我的编辑。顺便说一句,
选择
无效。
update the_table
  set the_column = coalesce(the_column, '{}')||'{"obj":{"a":1,"b":2}}'