-- 更新,整个json对象信息
-- 1
update products
set properties='
{"dimension":[1,2,3],"weight":10,"json":{"name":"sony"}
}'
where product_id=1;-- 2
update products
set properties=json_object(
'weight',19,
'dimensions',json_array(1,2,3),
'json',json_object('name','son')
)
where product_id=2; -- 更新或添加 json对象中的部分信息
update products
set properties = json_set(properties,'$.weight',20,'$.age',11
)
where product_id = 1;
/* json_set() 第一个参数是要更新的json对象,其他的就是正常的添加,修改了 */-- 删除 json对象中的部分信息
update products
set properties = json_remove(properties,'$.age'
)
where product_id = 1;-- 查询 列中json对象,json对象中的元素信息
-- 1
select product_id,json_extract(properties,'$.weight')
from products
where product_id in (1,2);
/* json_extract(properties,'$.weight')
第一个参数为json对象,也就是属性列
第二个参数是一条路径,一个字符串,即$.something
用'$' 表示当前的json文档,用'.'来访问单独的属性或键*/-- 2
select product_id,
properties -> '$.dimensions[1]',
properties -> '$.weight',
properties -> '$.json.name' ,
properties ->> '$.json.name'
from products
where product_id in (1,2)
/* '->' ,为列路径运算符'->>' ,在此基础上去掉引号*/