MySQL5.7 JSON类型及其相关函数的学习
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类型及其相关函数的学习的更多相关文章
- php类型的相关函数,运算符,条件判断,循环
类型的相关函数 函数的原型 :函数返回值类型 函数名(参数1类型 参数1,参数2类型 参数2--) 1, 任何一个函数,都要考虑它是否有返回值以及该返回值的类型,如果该函数没有返回值,就用void来 ...
- SpringMVC——对Ajax的处理(包含 JSON 类型)
一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...
- Struts2+Jquery实现ajax并返回json类型数据
来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...
- MySQL5.7 JSON实现简介
版权声明:本文由吴双桥原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/205 来源:腾云阁 https://www.qclo ...
- 转载:Struts2+Jquery实现ajax并返回json类型数据
摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...
- 使用mysql innodb 使用5.7的json类型遇到的坑和解决办法
---------------------------------------------- #查询JSON的某个字段 select data -> '$.Host' from temp #创建 ...
- springMVC参数绑定JSON类型的数据
需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...
- MySQL JSON 类型数据操作
1.Java 中动态扩展字段,会导致数据库表被锁,在MySQL 5.7.8版本之前,因为MySQL不能直接操作JSON类型数据,可以将一个字段设定成varchar类型,里面存放JSON格式数据,这样在 ...
- 腾讯云数据库团队:MySQL5.7 JSON实现简单介绍
作者介绍:吴双桥 腾讯云project师 阅读原文.很多其它技术干货.请訪问fromSource=gwzcw.57435.57435.57435">腾云阁. 本文主要介绍在MySQL ...
随机推荐
- 如何关闭git pull产生的merge 信息
编辑 ~/.gitconfig [core] mergeoptions = --no-edit 或者终端之行 git config --global core.mergeoptions --no-ed ...
- 【C++】满二叉树问题
/* 给出一棵满二叉树的先序遍历,有两种节点:字母节点(A-Z,无重复)和空节点(#).要求这个树的中序遍历.输出中序遍历时不需要输出#. 满二叉树的层数n满足1<=n<=5. Sampl ...
- 4. Scala程序流程控制
4.1 程序流程控制说明 在程序中,程序运行的流程控制决定程序是如何执行的,是我们必须掌握的,主要有三大流程控制语句,顺序控制,粉质控制,循环控制 温馨提示:Scala语言中控制结构和Java语言中的 ...
- Quartz任务调度
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! Quartz任务调度 课程目标 : 了解Quartz框架 : 任务(Job) 触发器(Trig ...
- xmanager 开启X11转发失败问题解决
安装相关rpm包 yum -y install xorg-x11-xauth xorg-x11-utils xorg-x11-fonts-* yum install dejavu-lgc-sans-f ...
- mysql关于排序值的问题,指定排序值
SELECT a.* FROM `catalog_eav_attribute` ea JOIN `eav_attribute` a ON ea.`attribute_id`=a.`attribute_ ...
- for里面是采用setInterval遍历二维数组,for循环到最后一个数的时候,才执行setInterval的问题解决
点击播放看效果 <!doctype html> <html lang="en"> <head> <meta charset="U ...
- 在VM虚拟机中安装Centos操作系统
首先我们要下载 Centos https://www.centos.org/ 这个是Centos官方 最新版本 7 https://www.centos.org/download/ 提供有 DVD安 ...
- iTerm2 与 Oh My Zsh的配套使用
详见: https://www.jianshu.com/p/7de00c73a2bb https://github.com/sirius1024/iterm2-with-oh-my-zsh https ...
- ADB——命令大全
基本语法 基本语法 adb [-d|-e|-s <serialNumber>] <command> # serialNumber表示设备序列号,也可以是ip地址 # 如果只有一 ...