postgresql - 如何在 postgresql 中使用 jsonb_set 更新空的 jsonb 列?

标签 postgresql sql-update jsonb

我的目的是使用 jsonb_set 更新 jsonb 列,该列当前为 null,并且对象具有多个键值对。更新命令成功执行,但它没有更新任何内容,该列仍然为空。我正在尝试以下查询。

UPDATE tab 
  set value = jsonb_set(value, '{}', '{"a" : 100, "b" : [100, 200]}'::jsonb) 
where id = 100;

有什么解决办法吗?

最佳答案

据我了解,这种情况似乎不需要 jsonb_set 。只需将字符串转换为 jsonb 即可更新

UPDATE tab 
  set value = '{"a" : 100, "b" : [100, 200]}'::jsonb
where id = 100 
--and value is null; --additional check if you need.

Demo

关于postgresql - 如何在 postgresql 中使用 jsonb_set 更新空的 jsonb 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54473517/

相关文章:

sql - 很难合并 jsonb 对象数组和列

postgresql - 对复杂jsonb内容进行全文检索

java - Postgres spring boot R2dbc应用程序中缺少DatabaseClient

postgresql - 有没有办法显示用户定义的 postgresql 枚举类型定义?

postgresql - Postgres JSONB 唯一约束

mysql - 如何在 MySQL 中更新具有不同值的多行?

部分改变列值的MySQL更新语句

MySQL错误: You can't specify target table 'users_words' for update in FROM clause

sql - LIMIT 和包含在单个 JSONB 查询中

json - 在 Postgres jsonb_to_recordset() 中显示为 NULL 的键值