mysql> CREATE TABLE `json_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` json NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> select * from json_table ;
+----+------------------------------+
| id | info |
+----+------------------------------+
| 1 | {"age": 24, "name": "lucy"} |
| 2 | {"age": 20, "name": "lili"} |
| 3 | {"age": 25, "name": "curry"} |
+----+------------------------------+
3 rows in set (0.00 sec)

Creating JSON Values

MySQL里的json分为json array和json object。

  • JSON_TYPE():查看字段类型

  • JSON_ARRAY():返回json数组

  • JSON_OBJECT():返回json对象

  • JSON_MERGE():合并多个json文档

举个栗子:

#查看类型
mysql> select id, json_extract(info, '$.name') as name , json_type(json_extract(info, '$.name')) as type from json_table where id=3 ;
+----+---------+--------+
| id | name | type |
+----+---------+--------+
| 3 | "curry" | STRING |
+----+---------+--------+
1 row in set (0.00 sec) #生成一个json数据组
mysql> select json_array('aaa', 'bbb', 'ddd') as array;
+-----------------------+
| array |
+-----------------------+
| ["aaa", "bbb", "ddd"] |
+-----------------------+
1 row in set (0.00 sec) #生成一个json对象
mysql> select json_object('aaa', 1, 'bbb', 2, 'ddd', 3) as array;
+--------------------------------+
| array |
+--------------------------------+
| {"aaa": 1, "bbb": 2, "ddd": 3} |
+--------------------------------+
1 row in set (0.00 sec) #将多个json文档合并
mysql> select json_merge(json_array('aaa', 'bbb', 'ddd'), json_object('aaa', 1, 'bbb', 2, 'ddd', 3)) as json ;
+-------------------------------------------------------+
| json |
+-------------------------------------------------------+
| ["aaa", "bbb", "ddd", {"aaa": 1, "bbb": 2, "ddd": 3}] |
+-------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

JSON查询

MySQL里的json分为json array和json object

对于json array,在索引数据时用从0开始的下标进行索引,$表示整个json对象,例如:$[0]$[1]

对于json object,在索引数据时用key进行索引,含有特殊字符的key要用""括起来,比如$."my name")

  • 提取json字段:json列->'$.键'JSON_EXTRACT(json列 , '$.键')

  • 去掉json字段双引号:JSON_UNQOUTE() 或者 json列->>'$.键'

提取JSON 字段的表达式可以用于SELECT查询列表 ,WHERE/HAVING , ORDER/GROUP BY语句中,JSON 中的元素搜索也是严格区分变量类型

json不同于字符串,不能当作字符串直接做比较,通过CAST()将字符串转换成JSON形式,再进行比较,后面举栗

举个栗子:

json列->'$.键' 查询:

mysql> select id, info->'$.name' as name from json_table ;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 2 | "lili" |
| 3 | "curry" |
+----+---------+
3 rows in set (0.00 sec) mysql> select id, info->'$.name' as name from json_table where info->'$.age' >=24;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 3 | "curry" |
+----+---------+
2 rows in set (0.00 sec) ##复杂情况下的查询,例如
mysql> select info from json_table where id=3;
+----------------------------------------------------------------+
| info |
+----------------------------------------------------------------+
| ["abc", {"xxx": [123, 456], "my key": "my value"}, [666, 100]] |
+----------------------------------------------------------------+
1 row in set (0.00 sec) ## 查询json array的某个值
mysql> select info->'$[0]' from json_table where id=3 ;
+---------------+
| info->'$[0]' |
+---------------+
| "abc" |
+---------------+
1 row in set (0.00 sec) ## 查询json array中的的json object
mysql> select info->'$[1]."my key"' from json_table where id=3 ;
+--------------------------+
| info->'$[1]."my key"' |
+--------------------------+
| "my value" |
+--------------------------+
1 row in set (0.00 sec)

JSON_EXTRACT(json列 , '$.键')查询:

mysql> select id, json_extract(info, '$.name') as name from json_table ;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 2 | "lili" |
| 3 | "curry" |
+----+---------+
3 rows in set (0.05 sec) mysql> select id, json_extract(info, '$.name') as name from json_table where json_extract(info, '$.age') >= 24;
+----+---------+
| id | name |
+----+---------+
| 1 | "lucy" |
| 3 | "curry" |
+----+---------+
2 rows in set (0.00 sec)

JSON_UNQOUTE()方法举栗:

mysql> select id, json_unquote(json_extract(info, '$.name')) as name from json_table ;
+----+-------+
| id | name |
+----+-------+
| 1 | lucy |
| 2 | lili |
| 3 | curry |
+----+-------+
3 rows in set (0.00 sec) mysql> select id, info->>'$.name' as name from json_table ;
+----+-------+
| id | name |
+----+-------+
| 1 | lucy |
| 2 | lili |
| 3 | curry |
+----+-------+
3 rows in set (0.00 sec)

JSON字段与字符串比较举栗:

mysql> select * from json_table where info = '{"age": 25, "name": "curry"}';
Empty set (0.00 sec) mysql> select * from json_table where info = cast('{"age": 25, "name": "curry"}' as JSON);
+----+------------------------------+
| id | info |
+----+------------------------------+
| 3 | {"age": 25, "name": "curry"} |
+----+------------------------------+
1 row in set (0.00 sec)

JSON的索引

现在MySQL不支持对JSON列进行索引,官网文档的说明是:

JSON columns cannot be indexed. You can work around this restriction by creating an index on a generated column that extracts a scalar value from the JSON column.

虽然不支持直接在JSON列上建索引,但MySQL规定,可以首先使用路径表达式对JSON文档中的标量值建立虚拟列,然后在虚拟列上建立索引。这样用户可以使用表达式对自己感兴趣的键值建立索引。

栗如,创建索引:

mysql> alter table json_table add name varchar(20) generated always as (info->'$.name') virtual ;
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> select * from json_table ;
+----+------------------------------+---------+
| id | info | name |
+----+------------------------------+---------+
| 1 | {"age": 24, "name": "lucy"} | "lucy" |
| 2 | {"age": 20, "name": "lili"} | "lili" |
| 3 | {"age": 25, "name": "curry"} | "curry" |
| 4 | {"age": 24, "name": "tom"} | "tom" |
| 5 | {"age": 24, "name": "jurry"} | "jurry" |
| 6 | {"age": 30, "name": "tmry"} | "tmry" |
+----+------------------------------+---------+
6 rows in set (0.00 sec) mysql> select * from json_table where name=""tom"";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tom""' at line 1
mysql> select * from json_table where name="\"tom\"";
+----+----------------------------+-------+
| id | info | name |
+----+----------------------------+-------+
| 4 | {"age": 24, "name": "tom"} | "tom" |
+----+----------------------------+-------+
1 row in set (0.00 sec)
mysql> alter table json_table add index name_idx(name) ;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table json_table \G
*************************** 1. row ***************************
Table: json_table
Create Table: CREATE TABLE `json_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` json NOT NULL,
`name` varchar(20) GENERATED ALWAYS AS (json_extract(`info`,'$.name')) VIRTUAL,
PRIMARY KEY (`id`),
KEY `name_idx` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
1 row in set (0.00 sec) mysql> alter table json_table rename index name_idx to idx_name ;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table json_table \G
*************************** 1. row ***************************
Table: json_table
Create Table: CREATE TABLE `json_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`info` json NOT NULL,
`name` varchar(20) GENERATED ALWAYS AS (json_extract(`info`,'$.name')) VIRTUAL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

修改

JSON_INSERT() 插入新字段,对于已存在的字段无法修改

#插入新字段
mysql> update json_table set info=json_insert(info, '$.sex', 'M');
Query OK, 6 rows affected (0.30 sec)
Rows matched: 6 Changed: 6 Warnings: 0 mysql> select * from json_table ;
+----+-----------------------------------------------------------+
| id | info |
+----+-----------------------------------------------------------+
| 1 | {"age": 24, "sex": "M", "name": "lucy"} |
| 2 | {"age": 20, "sex": "M", "name": "lili"} |
| 3 | {"age": 25, "sex": "M", "name": "curry"} |
| 4 | {"age": 24, "sex": "M", "name": "tom"} |
| 5 | {"age": 24, "sex": "M", "name": "jurry"} |
| 6 | {"age": 30, "sex": "M", "name": "tmry"} |
+----+-----------------------------------------------------------+
6 rows in set (0.00 sec) #对于已存在的字段无法修改
mysql> update json_table set info=json_insert(info, '$.name', 'LUCY') where id=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0 mysql> select * from json_table where id=1 ;
+----+-----------------------------------------------------------+
| id | info |
+----+-----------------------------------------------------------+
| 1 | {"age": 24, "sex": "M", "name": "lucy"} |
+----+-----------------------------------------------------------+
6 rows in set (0.00 sec)

JSON_SET() 插入新值字段,并覆盖已经存在字段的值

#覆盖已经存在字段的值
mysql> update json_table set info=json_set(info, '$.name', 'LUCY') where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from json_table where id=1;
+----+-----------------------------------------------------------+
| id | info |
+----+-----------------------------------------------------------+
| 1 | {"age": 24, "sex": "M", "name": "LUCY"} |
+----+-----------------------------------------------------------+
1 row in set (0.00 sec) #插入新值字段
mysql> update json_table set info=json_set(info, '$.class', 'python') where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from json_table where id=2;
+----+------------------------------------------------------------+
| id | info |
+----+------------------------------------------------------------+
| 2 | {"age": 20, "sex": "M", "name": "lili", "class": "python"} |
+----+------------------------------------------------------------+
1 row in set (0.00 sec)

JSON_REPLACE() 只替换存在的字段

mysql> update json_table set info=json_replace(info, '$.sex', 'W') where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from json_table where id = 2 ;
+----+------------------------------------------------------------+
| id | info |
+----+------------------------------------------------------------+
| 2 | {"age": 20, "sex": "W", "name": "lili", "class": "python"} |
+----+------------------------------------------------------------+
1 row in set (0.00 sec)

JSON_REMOVE() 删除 JSON 元素

mysql> update json_table set info=json_remove(info, '$.class') where id=2 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from json_table where id = 2 ;
+----+-----------------------------------------+
| id | info |
+----+-----------------------------------------+
| 2 | {"age": 20, "sex": "W", "name": "lili"} |
+----+-----------------------------------------+
1 row in set (0.00 sec)

MySQL5.7 JSON类型及其相关函数的学习的更多相关文章

  1. php类型的相关函数,运算符,条件判断,循环

    类型的相关函数 函数的原型  :函数返回值类型 函数名(参数1类型 参数1,参数2类型 参数2--) 1, 任何一个函数,都要考虑它是否有返回值以及该返回值的类型,如果该函数没有返回值,就用void来 ...

  2. SpringMVC——对Ajax的处理(包含 JSON 类型)

    一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...

  3. Struts2+Jquery实现ajax并返回json类型数据

    来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...

  4. MySQL5.7 JSON实现简介

    版权声明:本文由吴双桥原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/205 来源:腾云阁 https://www.qclo ...

  5. 转载:Struts2+Jquery实现ajax并返回json类型数据

    摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...

  6. 使用mysql innodb 使用5.7的json类型遇到的坑和解决办法

    ---------------------------------------------- #查询JSON的某个字段 select data -> '$.Host' from temp #创建 ...

  7. springMVC参数绑定JSON类型的数据

    需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...

  8. MySQL JSON 类型数据操作

    1.Java 中动态扩展字段,会导致数据库表被锁,在MySQL 5.7.8版本之前,因为MySQL不能直接操作JSON类型数据,可以将一个字段设定成varchar类型,里面存放JSON格式数据,这样在 ...

  9. 腾讯云数据库团队:MySQL5.7 JSON实现简单介绍

    作者介绍:吴双桥 腾讯云project师 阅读原文.很多其它技术干货.请訪问fromSource=gwzcw.57435.57435.57435">腾云阁. 本文主要介绍在MySQL ...

随机推荐

  1. linux挂载系统ios文件与gcc安装

    挂载方法: 1.将iso文件拷贝到某一目录下,(/test) 2.建立挂载点文件夹:mkdir  /mnt/iso1 3.进入 mount –o loop  /test/**.iso  /mnt/is ...

  2. Android平台targetSdkVersion设置及动态权限

    --关于Android动态权限和targetSdkVersion Android系统自6.0开始,提供动态权限机制,对于敏感权限(存储,定位,录音,拍照,录像等),需要在APP运行过程中动态向用户申请 ...

  3. 深度学习基础(四) Dropout_Improving neural networks by preventing co-adaptation of feature detectors

    该笔记是我快速浏览论文后的记录,部分章节并没有仔细看,所以比较粗糙. 从摘要中可以得知,论文提出在每次训练时通过随机忽略一半的feature detectors(units)可以极大地降低过拟合.该方 ...

  4. HotSpot设计原理与实现:一、初识HotSpot

    一.HotSpot内核模块组成和功能框架 1.HotSpot内核模块图 (1)Prims模块: (2)Service模块: (3)Runtime模块: 二.虚拟机生命周期(JVM初始化过程) 1.虚拟 ...

  5. mysql distinct()用法

    在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...

  6. Java数组之二维数组

    Java中除了一维数组外,还有二维数组,三维数组等多维数组.本文以介绍二维数组来了解多维数组. 1.二维数组的基础 二维数组的定义:二维数组就是数组的数组,数组里的元素也是数组. 二维数组表示行列二维 ...

  7. JavaScript 判断当前设备是否是移动端还是PC

    if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){ alert('移动端')}else { alert('PC端') }

  8. Java基础之数组详解

    数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java 语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如 numbers[1 ...

  9. JS中的闭包(转自他处)

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...

  10. centos7.2 Apache+PHP7.2+Mysql5.6环境搭建

    yum安装PHP7.2 由于linux的yum源不存在php7.x,所以我们要更改yum源:rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-re ...