json - Postgresql更新json数据属性

标签 json postgresql jsonb postgresql-9.4 postgresql-9.6

我创建了一个字段名称是结果,类型是文本。我只想更新列中的“lat”。当我使用此查询时,出现语法错误。我能怎么做?

列数据为

"{"lat":"48.00855","lng":"58.97342","referer":"https:\/\/abc.com\/index.php"}"

查询是

update public.log set (result::json)->>'lat'=123 where id=6848202

语法错误是

ERROR:  syntax error at or near "::"

最佳答案

使用 jsonb concatenation operator (Postgres 9.5+):

update log
set result = result::jsonb || '{"lat":"123"}'
where id = 6848202

Postgres 9.4 中使用 json_each()json_object_agg()(因为 jsonb_object_agg() 不存在在 9.4 中)。

update log
set result = (
    select json_object_agg(key, case key when 'lat' then '123' else value end)
    from json_each(result)
    )
where id = 6848202

两种解决方案都假定 json 列不为空。如果它不包含 lat 键,第一个查询将创建它,但第二个查询不会。

关于json - Postgresql更新json数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47454358/

相关文章:

javascript - 如何递归处理 JSON 数据并从函数返回处理后的 JSON?

php - 解析存储在mysql表中的json字段作为php变量

postgresql - 在准备好的语句中查找 Postgres 查询,Golang

postgresql - postgres如何用减号引用表名

sql - 使用 jsonb_set() 进行更新仅影响嵌套数组中的一个对象

java - 重用 Gson 解析的已经实例化的对象

json - 在 Swift 中访问*远程* JSON 深层嵌套对象

postgresql - 如何更新事件触发器?

arrays - 在 PostgreSQL 9.4 中更新 json 数组的某些数组元素

spring - 使用带有 Spring Data 和绑定(bind)参数的 Postgres JSONB 查询失败并出现 InvalidDataAccessApiUsageException