mysql 5.7起支持json数据类型的字段。json作为现在最为流行的数据交互形式,mysql也不断跟进,在5.7版本开始新增json数据类型。虽然现在的应用应该还比较少,但也说不准能成为一种趋势。先简单学习一下mysql对json数据类型的相关操作和一些内置函数(以下内容基mysql于8.0.13)。
ps: 以下内容有不合理的地方,比如实际写sql的时候关键字应该大写,禁用select *等,只是为了直观,见谅~
创建含有json字段的表
create table test_json ( `id` int auto_increment, `obj_json` json, `arr_json` json, primary key (`id`) )engine = innodb default charset = utf8mb4;
json字段无需设置长度,也不能设置默认值
插入json记录
mysql的json类型支持json数组和json对象
#json_array ["xin", 2019, null, true, false, "2019-5-14 21:30:00"] #json_object {"key1": "value", "key2": 2019, "time": "2015-07-29 12:18:29.000000"}
json_array和json_object值可为字符串,数值,null,时间类型,以及布尔值
json_object的键需为字符串类型
插入方式:直接通过字符串的形式
insert into test_json (obj_json, arr_json) values ('{"key1": "value", "key2": 2019, "time": "2015-07-29 12:18:29.000000"}', '["xin", 2019, null, true, false, "2019-5-14 21:30:00"]');
查询结果
select * from test_json
插入方式:通过json_object(),json_array()
insert into test_json (obj_json, arr_json) values (json_object('key1', 'insert by json_object', 'key2', 3.14159), json_array('go', 'ruby', 'java', 'php'));
查询结果
select * from test_json
ps:两种类型可嵌套使用。
查询含json类型的字段
前面可以通过select语句查询含有json的记录,结果如上所示。
如果要提取json字段中具体值呢?
object类型
col->path形式,其中表达式path为$.key
select obj_json->'$."key1"' key1, obj_json->'$."key2"' key2 from test_json;
array类型
col->path形式,其中表达式path为$[index]
select arr_json->'$[0]' index1, arr_json->'$[1]' index2 , arr_json->'$[2]' index3 from test_json;
json类型字段的更新
更新你当然可以直接通过update覆盖掉整个字段
接下来简单介绍一下对json内的更新
内置函数json_set(),json_insert(),json_replace(),json_remove()
json_set() 插入值,如果存在则进行覆盖
update test_json set obj_json = json_set(obj_json, '$."json_set_key"', 'json_set_value', '$.time', 'new time'), arr_json = json_set(arr_json, '$[6]', 'seven element', '$[0]', 'replace first') where id = 1;
select * from test_json where id = 1;
json_insert()插入值,不会覆盖原有值
update test_json set obj_json = json_insert(obj_json, '$."json_insert_key"', 'json_insert_value', '$."key1"', 'set existing key'), arr_json = json_insert(arr_json, '$[4]', 'json_insert_value', '$[0]', 'set existing index') where id = 2;
select * from test_json where id = 2;
json_replace()只会覆盖原来有的值
update test_json set obj_json = json_replace(obj_json, '$."key1"', 'json_replace_key1', '$."json_replace_insert"', 'test'), arr_json = json_replace(arr_json, '$[3]', 'php is best language!', '$[5]', 'json_replace_insert') where id = 2;
select * from test_json where id = 2;
json_remove()移除
update test.test_json set obj_json = json_remove(obj_json, '$."key1"', '$."nonexistent key"'), arr_json = json_remove(arr_json, '$[0]', '$[5]') where id = 2;
select * from test_json where id = 2;
其他
如果存储的json有引号?
插入时需要进行转义
insert into test_json (obj_json, arr_json) values ('{"key1": "test_obj_value1\\""}', '["\\"test_arr_value1\\""]'); insert into test_json (obj_json, arr_json) values (json_object('key1', '\"test_obj_value1 \'single\''), json_array('\"test_arr_value1\" \'single\''));
查询
select obj_json->'$."key1"' obj_key1, arr_json->'$[0]' arr_index1 from test_json where id in (3, 4);
如果查询结果不想保留转义可采用col->>path的形式
select obj_json->>'$."key1"' obj_key1, arr_json->>'$[0]' arr_index1 from test_json where id in (3, 4);
合并函数json_merge_preserve()和json_merge_patch()
8.0之后提供了合并函数,可将多个json进行合并
区别在于json_merge_patch()会将原有值覆盖,而json_merge_preserve()不会
下面进行试验一下(不插入表)
select json_merge_patch( json_object('obj_key1', 'obj_value1', 'obj_key2', 'obj_value2'), json_object('obj_key2', 'new_obj_value2') ) as col1, json_merge_patch( json_array('arr_index1', 'arr_index2', 'arr_index3'), json_array('arr_index4', 'arr_index5', 'arr_index6') ) as col12;
select json_merge_preserve( json_object('obj_key1', 'obj_value1', 'obj_key2', 'obj_value2'), json_object('obj_key2', 'new_obj_value2') ) as col2, json_merge_preserve( json_array('arr_index1', 'arr_index2', 'arr_index3'), json_array('arr_index4', 'arr_index5', 'arr_index6') ) as col3;
如前面所说,json数据类型支持嵌套,简单演示一下
select json_merge_preserve( json_array('arr_index1', 'arr_index2'), json_object('obj_key1', 'obj_value1') ) as col1, json_merge_preserve( json_object('obj_key1', 'obj_value1'), json_array('arr_index1', 'arr_index2') ) as col2;
写在最后
json类型字段的使用,应当认真考虑数据库设计,看看适不适合应用json数据类型。开发往往结合其他语言使用,mysql作为一款trdb,有时候对json数据类型操纵会比较繁琐,如强类型语言的orm映射,是否使用json数据类型,还需结合实际情况斟酌。
到此这篇关于mysql json类型字段的简单使用的文章就介绍到这了,更多相关mysql json使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论