gpt4 book ai didi

postgresql - postgres 查询更新 json 字段

转载 作者:行者123 更新时间:2023-11-29 13:10:46 25 4
gpt4 key购买 nike

在 PostgreSQL 数据库中,我有一个名为 hotel 的表,其中有一列名为 hotel_info。此 hotel_info 列存储如下所示的 json 对象

{
"hotel":{
"room_details":{
"customer_detail":{
"first_name" : "aaa"
}
}
}}

我需要一个更新查询来更新“hotel_info”列中“first_name”字段的值。请帮助我为此建立查询。

提前致谢!

最佳答案

这样的事情应该可行(假设您使用的是确实支持 JSON 运算符的 Postgres 版本)。请注意,此处的“设置”功能仅适用于 JSONB,因此您可能需要在之前/之后转换列:

SELECT JSONB_SET(
hotel_info #> '{hotel, room_details, customer_detail}'
, '{first_name}'
, '"bbb"'
, create_missing FALSE) AS hotel_info_modified
FROM table

为了举例,我在这里将名称更改为“bbb”。通过 SELECT 检查这确实是预期的行为,然后您可以在需要时更改为 UPDATE。

关于postgresql - postgres 查询更新 json 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55072421/

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