测试环境: mysql8.0.19
准备工作create table json_demo ( 	`id` int ( 11 ) not null primary key, 	`content` json not null );insert into json_demo ( id, content )values	/*这条是数组*/	( 1, '[{"key": 1, "order": 1, "value": "34252"},{"key": 2, "order": 2, "value": "23423"}]' ),	/*这条是数组*/	( 2, '[{"key": 4, "order": 4, "value": "234"},{"key": 5, "order": 5, "value": "234324523"}]' ),	/*这条是对象*/	( 3, '{"key": 3, "order": 3, "value": "43242"}' ),	/*这条是对象*/	( 4, '{"key": 6, "order": 6, "value": "5423"}' );
json对象基础操作查询指定字段值
/* 基础查询 */select	content -> '$.key' as 'key',	json_extract(content, '$.key') as 'key2',	content -> '$.value' as 'value',	json_extract(content, '$.value') as 'value2',	content ->> '$.value' as 'value3',	json_unquote(json_extract(content, '$.value')) as 'value4'from	json_demo where	id > 2;
tips:
->和->>是mysql设计的语法,其中->在mysql5.7支持,->>在mysql8.0中支持。
->等效于json_extract(),当查询字段为字符串时,其返回值还会带有""。
->>等效于json_unquote(json_extract()),当查询字段为字符串时,其返回值不会带有""。
用于条件查询
content -> '$.key'可以看成一个字段,一个字段能做的操作基本他都能。
select	id,	content -> '$.key' as 'key',	content ->> '$.value' as 'value3'from	json_demo where	id > 2	and content -> '$.key' > 1	and content -> '$.value' like '%2%';
修改指定字段值
/* 修改 */update json_demo set content = json_replace(	content,	/* 将content.key值 + 1 */	'$.key', content -> '$.key' + 1,	/* 将content.value值后拼接'abc' */	'$.value', concat(content ->> '$.value', 'abc')) where id = 3;/* json_set也可以 */update json_demo set content = json_set(	content,	/* 将content.key值 + 1 */	'$.key', content -> '$.key' + 1,	/* 将content.value值后拼接'abc' */	'$.value', concat(content ->> '$.value', 'abc')) where id = 3;/* 查询修改结果 */select id,content,content -> '$.key' as 'key',content ->> '$.value' as 'value3'from json_demo where id = 3;/* 重新赋值 */update json_demo set content = json_replace(content,'$.key',3,'$.value','43242') where id = 3;
tips:
json_replace和json_set都可以用来修改某个字段值,区别在于json_replace替换不存在的属性时操作无效;而json_set则会将这个不存在的属性插入进去。
所以json_set也可以用来追加属性,与json_insert类似。区别在于json_insert如果插入一个已存在的属性时操作会失效,而json_set会替换。
追加元素
update json_demo set content = json_insert(content, '$.key', 234)where id = 3;select id,content,content -> '$.key' as 'key' from json_demo where id = 3;update json_demo set content = json_insert(content, '$.temp', 234)where id = 3;select id,content,content -> '$.key' as 'key' from json_demo where id = 3;update json_demo set content = json_set(content, '$.temp2', 432)where id = 3;select id,content,content -> '$.key' as 'key' from json_demo where id = 3;
json数组操作查询指定字段值
select	id,	content -> '$[*].key' as 'key',	content ->> '$[*].value' as 'value',	content -> '$[0].key' as 'key2',	content ->> '$[0].value' as 'value2',	/* 查询数组长度 */	json_length(content) as 'length'from	json_demo where	id < 3;
用于条件查询
select	id,	content -> '$[*].key' as 'key',	content ->> '$[*].value' as 'value'from	json_demo where	id < 3	/* content.value的值中存在like'%34%'的值 */	and content ->> '$[*].value' like '%34%'	/* content.key的值中有4 */	and json_overlaps(content ->> '$[*].key', '4' );
修改指定字段值
基础操作都跟json对象差不太多,就是在'$'后面加对应的索引位'$[0]',指定所有则'$[*]'。如果数组中包含数组,可以通过'$[1][2][3]'这种方式指定深层的数组元素。
追加元素
json_array_append和json_array_insert都可以实现数组元素追加。区别在于json_array_append可以不指定索引位,此时往最后位置追加;json_array_insert必须指定索引位,不指定则会报错。
json_array_append是追加在指定索引位后面,而json_array_insert则是插入到指定索引位前面。
更多操作名称描述
json_array() 创建json数组 
json_array_append() 将数据附加到json文档 
json_array_insert() 插入json数组 
json_contains() json文档是否在路径中包含特定对象 
json_contains_path() json文档是否在路径中包含任何数据 
json_depth() json文档的最大深度 
json_extract() 从json文档返回数据 
json_insert() 将数据插入json文档 
json_keys() json文档中的键数组 
json_length() json文档中的元素数 
json_merge() (已弃用) 合并json文档,保留重复的键。json_merge_preserve()的已弃用同义词 
json_merge_patch() 合并json文档,替换重复键的值 
json_merge_preserve() 合并json文档,保留重复的键 
json_object() 创建json对象 
json_overlaps() (8.0.17引入) 比较两个json文档,如果它们具有共同的任何键值对或数组元素,则返回true(1),否则返回false(0) 
json_pretty() 以易于阅读的格式打印json文档 
json_quote() 引用json文档 
json_remove() 从json文档中删除数据 
json_replace() 替换json文档中的值 
json_schema_valid() (8.0.17引入) 根据json模式验证json文档;如果文档针对架构进行了验证,则返回true / 1;否则,则返回false / 0。 
json_schema_validation_report() (8.0.17引入) 根据json模式验证json文档;以json格式返回有关验证结果的报告,包括成功或失败以及失败原因 
json_search() json文档中值的路径 
json_set() 将数据插入json文档 
json_storage_free() 部分更新后,json列值的二进制表示形式中的可用空间 
json_storage_size() 用于存储json文档的二进制表示形式的空间 
json_table() 从json表达式返回数据作为关系表 
json_type() json值类型 
json_unquote() 取消引用json值 
json_valid() json值是否有效 
json_value() (8.0.21引入) 在提供的路径所指向的位置从json文档中提取值;以varchar(512)或指定的类型返回此值 
member of() (8.0.17引入) 如果第一个操作数与作为第二个操作数传递的json数组的任何元素匹配,则返回true(1),否则返回false(0)
以上就是mysql之json类型字段如何使用的详细内容。
   
 
   