MySQL中JSON函数用法示例及实战示例
-
创建JSON值
-
合并JSON值
-
搜索JSON值
- 路径语法
-
提取JSON值
-
修改JSON值
-
JSON值的比较和排序
-
JSON值和非JSON值转换
-
实战示例
- 是否已安排专属技师
- 专属技师:可多选,但选项相互取交集(例如选择A和B和C,则相应的客户应为同时被A、B、C跟进的客户);
- find_in_set不能同时比较多个值,所以使用concat将group_concat返回的数据拼接成JSON格式,再使用json_contains函数查询当前JSON文档中是否包含指定的JSON数组(’[]’:要用单引号和中括号)
select car_owner_user_id
from r_followup
where 1 = 1
and delete_status = 0
group by car_owner_user_id
having json_contains(concat('[', group_concat(distinct staff_base_id), ']'), '[120, 119]')
测试根据json数组的字段查询
[
{
"id": "141" ,
"name": "xxx",
"type": "input"
},
{
"id": "142",
"name": "xin",
"type": "textarea"
}
]
查询json数组里面对象的id等于142的记录
用JSON_CONTAINS(字段,JSON_OBJECT(‘json属性’, “内容”))
select * from log2 where JSON_CONTAINS(data,JSON_OBJECT('id', "142"))