gpt4 book ai didi

json - Postgresql更新json数据属性

转载 作者:行者123 更新时间:2023-11-29 11:31:19 28 4
gpt4 key购买 nike

我创建了一个字段名称是结果,类型是文本。我只想更新列中的“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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com