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. oracle中查询用户信息

    1.查看所有用户: select * from dba_users; select * from all_users; select * from user_users; 2.查看用户或角色系统权限( ...

  2. 最大流sap

    带当前弧优化 gap优化的sap 甚至省去了开始的bfs分层 虽然花了一些时间了解原理 但是感觉不亏 现在能完全独立靠原理写出具体实现了 #include<cstdio> #include ...

  3. CentOS系统下安装python3+Django

    转载:CentOS系统下安装python3+Django 1.首先用yum安装下vim,因为CentOS 7可能根本没自带完整vim,经常出现输入乱码:yum -y install vim 2.安装开 ...

  4. Ehcache 3.7文档—基础篇—XML Configuration

    你可以使用xml配置创建CacheManager,根据这个schema definition ( http://www.ehcache.org/documentation/3.7/xsds.html# ...

  5. js canvas游戏初级demo-躲避障碍物

    在线演示地址 http://200ok.fun:3100/html/game_demo.html 继上次js canvas游戏初级demo-上下左右移动(https://www.cnblogs.com ...

  6. VisualStudioCode中用dotnet命令创建多个ASP.NET Core 项目、类库、控制台程序,并添加应用间的引用

    一.准备工作 首先安装VisualStudioCode并且可以使用. 1.首先新创建空的MyApps文件夹,作为项目主目录,下面将在这个文件夹中创建多个web应用程序.类型.控制台程序等. 2.打开V ...

  7. Mybatis-Plus 3.0代码生成器

    package com.kyplatform.generator; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusExcepti ...

  8. 改造一下jeecg中的部门树

    假装有需求 关于 jeecg 提供的部门树,相信很多小伙伴都已经用过了,今天假装有那么一个需求 "部门树弹窗选择默认展开下级部门",带着这个需求再次去探索一下吧. 一.改造之前的部 ...

  9. Django---请求、响应

    Django的请求和响应 一.客户端向服务器的请求简介 二.响应对象构造 回到顶部 一.客户端向服务器的请求简介 1.c-->s的传参的4中途径 提取的URL的特定部分,如/天气/北京/ 201 ...

  10. [py]python操作zookeeper

    参考: https://blog.csdn.net/heizistudio/article/details/79568188 1.安装zookeeper zookeeper-3.4.6.tar.gz ...