mysql 表查询json数据
// An highlighted block
字段attributes的值为
{“desc”: “{ “index”:“序号”, “startDate”:“开始日期”, “endDate”:“结束日期”, “ruId”:“发奖规则” }”, “name”: “日期配置”, “tips”: “必配”, “type”: “JSON”, “value”: “[{“index”:1,“startDate”:“2022-02-15”,“endDate”:“2024-03-19”,“ruId”:“ru_4”}]”, “variable”: “date_config”, “configType”: “”}
1、查找desc,使用json_extract
select json_extract(attributes,’$.desc’) from TableName
若desc下有多个元素,可使用数组下标,desc[0]/desc[1]取对应元素;若desc下包含多个键值对,则使用desc.key取对应key值
2、替换可使用json_replace,json_set
二者区别
json_set() 若查找的值存在则替换,不存在则插入
json_replace()替换/修改存在的值
替换name的值
update TableName set name=json_set(attributes,’$.name’,‘新的值’)
update TableName set name=json_replace(attributes,’$.name’,‘新的值’)
select * from fa_trade_order WHERE json_extract(json,'$.status') ="1"