当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL操作之JSON数据类型操作详解

MySQL操作之JSON数据类型操作详解

2017年11月30日  | 移动技术网IT编程  | 我要评论

上一篇文章我们介绍了,今天我们看看mysql操作之json数据类型的相关内容。

概述

mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据时,还是比较别扭的。

创建一个json字段的表

首先先创建一个表,这个表包含一个json格式的字段:

create table table_name (
  id int not null auto_increment, 
  json_col json,
  primary key(id)
);

上面的语句,主要注意json_col这个字段,指定的数据类型是json。

插入一条简单的json数据

insert into
  table_name (json_col) 
values
  ('{"city": "galle", "description": "best damn city in the world"}');
  

上面这个sql语句,主要注意values后面的部分,由于json格式的数据里,需要有双引号来标识字符串,所以,values后面的内容需要用单引号包裹。

插入一条复杂的json数据

insert into table(col) 
values('{"opening":"sicilian","variations":["pelikan","dragon","najdorf"]}');

这地方,我们插入了一个json数组。主要还是注意单引号和双引号的问题。

修改json数据

之前的例子中,我们插入了几条json数据,但是如果我们想修改json数据里的某个内容,怎么实现了?比如我们向 variations 数组里增加一个元素,可以这样:

update myjson set dict=json_array_append(dict,'$.variations','scheveningen') where id = 2;

这个sql语句中,$符合代表json字段,通过.号索引到variations字段,然后通过json_array_append函数增加一个元素。现在我们执行查询语句:

select * from myjson

得到的结果是:

+----+-----------------------------------------------------------------------------------------+
| id | dict                                          |
+---+-----------------------------------------------------------------------------------------+
| 2 | {"opening": "sicilian", "variations": ["pelikan", "dragon", "najdorf", "scheveningen"]} |
+----+-----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

关于mysql中,json数据的获取方法,参照官方链接json path syntax

创建索引

mysql的json格式数据不能直接创建索引,但是可以变通一下,把要搜索的数据单独拎出来,单独一个数据列,然后在这个字段上键一个索引。下面是官方的例子:

mysql> create table jemp (
  ->   c json,
  ->   g int generated always as (c->"$.id"),
  ->   index i (g)
  -> );
query ok, 0 rows affected (0.28 sec)

mysql> insert into jemp (c) values
   >  ('{"id": "1", "name": "fred"}'), ('{"id": "2", "name": "wilma"}'),
   >  ('{"id": "3", "name": "barney"}'), ('{"id": "4", "name": "betty"}');
query ok, 4 rows affected (0.04 sec)
records: 4 duplicates: 0 warnings: 0

mysql> select c->>"$.name" as name
   >   from jemp where g > 2;
+--------+
| name  |
+--------+
| barney |
| betty |
+--------+
2 rows in set (0.00 sec)

mysql> explain select c->>"$.name" as name
   >  from jemp where g > 2\g
*************************** 1. row ***************************
      id: 1
 select_type: simple
    table: jemp
  partitions: null
     type: range
possible_keys: i
     key: i
   key_len: 5
     ref: null
     rows: 2
   filtered: 100.00
    extra: using where
1 row in set, 1 warning (0.00 sec)

mysql> show warnings\g
*************************** 1. row ***************************
 level: note
  code: 1003
message: /* select#1 */ select json_unquote(json_extract(`test`.`jemp`.`c`,'$.name'))
as `name` from `test`.`jemp` where (`test`.`jemp`.`g` > 2)
1 row in set (0.00 sec)

这个例子很简单,就是把json字段里的id字段,单独拎出来成字段g,然后在字段g上做索引,查询条件也是在字段g上。

字符串转json格式

把json格式的字符串转换成mysql的json类型:

select cast('[1,2,3]' as json) ;
select cast('{"opening":"sicilian","variations":["pelikan","dragon","najdorf"]}' as json);

所有mysql json函数

name description
json_append() append data to json document
json_array() create json array
json_array_append() append data to json document
json_array_insert() insert into json array-> return value from json column after evaluating path; equivalent to json_extract().
json_contains() whether json document contains specific object at path
json_contains_path() whether json document contains any data at path
json_depth() maximum depth of json document
json_extract() return data from json document->> return value from json column after evaluating path and unquoting the result; equivalent to json_unquote(json_extract()).
json_insert() insert data into json document
json_keys() array of keys from json document
json_length() number of elements in json document
json_merge() merge json documents, preserving duplicate keys. deprecated synonym for json_merge_preserve()
json_merge_preserve() merge json documents, preserving duplicate keys
json_object() create json object
json_quote() quote json document
json_remove() remove data from json document
json_replace() replace values in json document
json_search() path to value within json document
json_set() insert data into json document
json_type() type of json value
json_unquote() unquote json value
json_valid() whether json value is valid

总结

以上就是本文关于mysql操作之json数据类型操作详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:、简述redis和mysql的区别几个比较重要的mysql变量等,如有不足之处,欢迎留言指出,小编会及时回复大家并进行修改,努力为广大编程爱好及工作者提供更好的文章和阅读体验。下面推荐几本跟mysql操作有关的书籍,供大家参考:

mysql数据库应用从入门到精通(第2版) pdf扫描版

mysql5 权威指南(第3版)中文版 pdf扫描版

希望大家能够喜欢,更多精彩内容尽在:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网