如何对带有数字键的文档使用MySQL JSON_SET?

时间:2018-10-02 14:51:00

标签: mysql

我在mysql表中有一个带有以下示例数据的json字段名称shipping_info

{
    "shipping": {
        "0": {
            "phone": "(222) 222-2222",
            "address1": "streeta",
            "address2": null,
            "address3": null,
            "attention": "company",
            "phone_ext": null,
            "postal_code": "91406",
            "proper_city": "VAN NUYS",
            "address_type": "u",
            "city_or_town": "VAN NUYS",
            "country_code": "US"
        }
    }
}

我的目标是更新此路径shipping.0.address1中的值。 到目前为止,这是我的查询,但似乎无效

UPDATE cart_items 
SET shipping_info = JSON_SET(
    shipping_info, 
    '$.shipping[0].address1', 'new value'
) 
WHERE cart_item_id= 1;

1 个答案:

答案 0 :(得分:0)

我能够通过以下查询进行操作:

UPDATE cart_items 
SET shipping_info = JSON_SET(
   shipping_info, 
   '$.shipping."0".address1', 'new value'
) 
WHERE cart_item_id= 1;

我将其发布为答案,以防万一有人也会遇到相同的情况。