mysql5.7新增加的JSON数据类型特征介绍

发布时间:2020-05-13 17:41:41

来源:亿速云

阅读:139

作者:三月

下面一起来了解下mysql5.7新增加的JSON数据类型特征,相信大家看完肯定会受益匪浅,文字在精不在多,希望mysql5.7新增加的JSON数据类型特征这篇短内容是你想要的。

一、json结构

创建测试表CREATE TABLE `article` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,

`category` json NOT NULL,

`tags` json NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

分析:article表中的字段category与tags均为json类型

填写测试数据INSERT INTO `article` VALUES

(1,'{\"id\": 1, \"name\": \"php\"}','[\"php\", \"mysql\", \"linux\", \"nginx\", \"redis\", \"memcache\", \"mongodb\"]'),

(2,'{\"id\": 2, \"name\": \"java\"}','[\"java\", \"mysql\", \"oracel\", \"linux\", \"nginx\", \"redis\", \"memcache\", \"mongodb\"]'),

(3,'{\"id\": \"3\", \"name\": \"c#\"}','[\"c\", \"c++\", \"OS\", \"linux\", \"unix\", \"IBM\"]');

总体预览

二、json查询

select id,json_extract(category,'$.name') as name from test.article;#提取json字段里面的信息

# column->path形式 访问json中的元素 category->'$.name'

select id,category->'$.name' as name from test.article;#提取json字段里面的信息(访问json中的元素 category->'$.name')

select id,json_unquote(json_extract(category,'$.name')) as name from test.article;#提取json字段里面的信息,json_unqoute去双引号

select id,json_unquote(category->'$.name') as name from test.article;#提取json字段里面的信息,json_unqoute去双引号

select id,category->>'$.name' as name from test.article;

select * from test.article where category='{"id": 1, "name": "php"}'; #json不同于字符串,不能当作字符串做比较

select * from test.article where category=cast('{"id": 1, "name": "php"}' as JSON); #通过CAST将字符串转换成JSON形式

select * from test.article where category->'$.name'='java';

select * from test.article where category->>'$.name'='java';

#JSON 中的元素搜索是严格区分变量类型的,比如说整型和字符串是严格区分的

select * from test.article where category->'$.id'='2';#字符号串

select * from test.article where category->'$.id'=2;#×××

select * from test.article where category->'$.id'='3';#字符号串

select * from test.article where json_extract(category,'$.id')='3';#字符号串

select * from test.article where json_contains(category,'2','$.id');#整数

select * from test.article where json_contains(category,'"3"','$.id');#字符号串

select * from test.article where json_contains(tags,'"linux"');#字符号串

2、查询json格式的字段

mysql> select jsn_extract(data, '.name′),jsnextract(data,′.address') from user;

+-----------------------------+-------------------------------+

| jsn_extract(data, '.name′)|jsnextract(data,′.address') |

+-----------------------------+-------------------------------+

| "David" | "Shangahai" |

| "Amy" | NULL |

+-----------------------------+-------------------------------+

2 rows in set (0.00 sec)

3、给json格式的某个键字段创建索引。首先创建虚拟列,之后在改虚拟列上创建索引。

mysql> ALTER TABLE user ADD user_name varchar(128)

-> GENERATED ALWAYS AS (jsn_extract(data,'$.name')) VIRTUAL;

Query OK, 0 rows affected (0.01 sec)

Records: 0 Duplicates: 0 Warnings: 0

mysql> select user_name from user;

+-----------+

| user_name |

+-----------+

| "Amy"     |

| "David"   |

+-----------+

2 rows in set (0.00 sec)

mysql> alter table user add index idx_username (user_name);

Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0

4、之后通过虚拟列名对json特定列进行索引查询:

mysql> explain select * from user where user_name='"Amy"'\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: user

partitions: NULL

type: ref

possible_keys: idx_username

key: idx_username

key_len: 131

ref: const

rows: 1

filtered: 100.00

Extra: NULL

1 row in set, 1 warning (0.00 sec)

三、json更新

更新 JSON

如果是整个 json 更新的话,和插入时类似的。mysql> UPDATE lnmp SET tags = '[1, 3, 4]' WHERE id = 1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT * FROM lnmp;

+----+------------------------------+-----------+

| id | category                     | tags      |

+----+------------------------------+-----------+

| 1  | {"id": 1, "name": "lnmp.cn"} | [1, 3, 4] |

| 2  | {"id": 2, "name": "php.net"} | [1, 3, 5] |

+----+------------------------------+-----------+

2 rows in set (0.00 sec)

但如果要更新 JSON 下的元素,MySQL 并不支持 column->path 的形式mysql> UPDATE lnmp SET category->'$.name' = 'lnmp', tags->'$[0]' = 2 WHERE id = 1;

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 '->'$.name' = 'lnmp', tags->'$[0]' = 2 WHERE id

= 1' at line 1

则可能要用到以下几个函数

JSON_INSERT() 插入新值,但不会覆盖已经存在的值mysql> UPDATE lnmp SET category = JSON_INSERT(category, '$.name', 'lnmp', '$.url', 'www.lnmp.cn') WHERE id = 1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT * FROM lnmp;

+----+----------------------------------------------------+-----------+

| id | category                                           | tags      |

+----+----------------------------------------------------+-----------+

|  1 | {"id": 1, "url": "www.lnmp.cn", "name": "lnmp.cn"} | [1, 3, 4] |

|  2 | {"id": 2, "name": "php.net"}                       | [1, 3, 5] |

+----+----------------------------------------------------+-----------+

2 rows in set (0.00 sec)

可以看到 name 没有被修改,但新元素 url 已经添加进去

JSON_SET() 插入新值,并覆盖已经存在的值mysql> UPDATE lnmp SET category = JSON_SET(category, '$.host', 'www.lnmp.cn', '$.url', 'http://www.lnmp.cn') WHERE id = 1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT * FROM lnmp;

+----+----------------------------------------------------------------------------------+-----------+

| id | category                                                                         | tags      |

+----+----------------------------------------------------------------------------------+-----------+

|  1 | {"id": 1, "url": "http://www.lnmp.cn", "host": "www.lnmp.cn", "name": "lnmp.cn"} | [1, 3, 4] |

|  2 | {"id": 2, "name": "php.net"}                                                     | [1, 3, 5] |

+----+----------------------------------------------------------------------------------+-----------+

2 rows in set (0.00 sec)

可以看到 host 已经插入,url 已经被修改

JSON_REPLACE() 只替换存在的值mysql> UPDATE lnmp SET category = JSON_REPLACE(category, '$.name', 'php', '$.url', 'http://www.php.net') WHERE id = 2;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT * FROM lnmp;

+----+----------------------------------------------------------------------------------+-----------+

| id | category                                                                         | tags      |

+----+----------------------------------------------------------------------------------+-----------+

|  1 | {"id": 1, "url": "http://www.lnmp.cn", "host": "www.lnmp.cn", "name": "lnmp.cn"} | [1, 3, 4] |

|  2 | {"id": 2, "name": "php"}                                                         | [1, 3, 5] |

+----+----------------------------------------------------------------------------------+-----------+

2 rows in set (0.00 sec)

可以看到 name 已经被替换,url 不存在被忽略。

JSON_REMOVE() 删除 JSON 元素mysql> UPDATE lnmp SET category = JSON_REMOVE(category, '$.url', '$.host') WHERE id = 1;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT * FROM lnmp;

+----+------------------------------+-----------+

| id | category                     | tags      |

+----+------------------------------+-----------+

|  1 | {"id": 1, "name": "lnmp.cn"} | [1, 3, 4] |

|  2 | {"id": 2, "name": "php"}     | [1, 3, 5] |

+----+------------------------------+-----------+

2 rows in set (0.00 sec)

更多函数请参考:http://dev.mysql.com/doc/refman/5.7/en/json-modification-functions.html

MySQL JSON 在 PHP 中的表现

虽然在 MySQL 是个JSON 类型,但实际在 PHP 应用中返回的是 JSON 格式的字符串array(2) {

[0]=>

array(3) {

["id"]=>

string(1) "1"

["category"]=>

string(28) "{"id": 1, "name": "lnmp.cn"}"

["tags"]=>

string(9) "[1, 3, 4]"

}

[1]=>

array(3) {

["id"]=>

string(1) "2"

["category"]=>

string(24) "{"id": 2, "name": "php"}"

["tags"]=>

string(9) "[1, 3, 5]"

}

}

看完mysql5.7新增加的JSON数据类型特征这篇文章后,很多读者朋友肯定会想要了解更多的相关内容,如需获取更多的行业信息,可以关注我们的行业资讯栏目。

mysql5.7 json特性_mysql5.7新增加的JSON数据类型特征介绍相关推荐

  1. mysql json invalid json text_MySQL 5.7新增加的json数据类型

    MySQL 5.7中有json存储类型了以前我们只能通过php来进行序列化了不过现在就不需要了我们可以直接使用MySQL 5.7的json数据类型来存储json格式数据了,具体来看介绍. 在MySQL ...

  2. mysql5.7无损复制_MySQL5.7新特性:lossless replication 无损复制

    MySQL的三种复制方式 asynchronous 异步复制 fully synchronous 全同步复制 Semisynchronous 半同步复制 asynchronous replicatio ...

  3. mysql 5.6 json处理_mysql5.6版本怎么对json进行截取某个具体的值

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  4. mysql5.7 json特性_【Mysql】Mysql5.7新特性之-json存储

    一 写在前面 本系列文章基于 5.7.12 版本讲述MySQL的新特性.从安装,文件结构,SQL ,优化 ,运维层面 复制,GITD等几个方面展开介绍 5.7 的新特性和功能.同时也建议大家跟踪官方b ...

  5. mysql5.7.20新特_Mysql5.7新特性

    Mysql在被sun,继而被oracle收购之后,并没有被oracle这个巨人雪藏,反而近几年的发展速度极为之快,谁都知道,你oracle做不好mysql,开源的市场就会被别人取代,pg,MariaD ...

  6. 总结PHP 7新增加的特性

    ?? 运算符(NULL 合并运算符) 把这个放在第一个说是因为我觉得它很有用.用法: $a = $_GET['a'] ?? 1;它相当于: <?PHP $a = isset($_GET['a'] ...

  7. mysql5.0版本特性_mysql各版本的新特性整理

    mysql各版本的新特性整理 一.各版本的常用命令差异 show innodb status\G mysql-5.1 show engines innodb status\G mysql-5.5 关于 ...

  8. 支持的sql语法_PostgreSQL 12 新特性解读之一|支持 SQL/JSON path

    作者介绍 谭峰,网名francs,<PostgreSQL实战>作者之一,<PostgreSQL 9 Administration Cookbook>译者之一,PostgreSQ ...

  9. sql 截取json数据_PostgreSQL 12 新特性解读之一|支持 SQL/JSON path

    作者介绍 谭峰,网名francs,<PostgreSQL实战>作者之一,<PostgreSQL 9 Administration Cookbook>译者之一,PostgreSQ ...

最新文章

  1. Android之自定义控件显示点赞用户并通过用户名称进入该用户主页的功能
  2. 学会python编程容易吗-Python为什么这么火?小孩子适合学习python编程吗?
  3. nyoj1307Linux的文件权限对不对
  4. 关于Exchang server 2010 MCITP
  5. iOS-UICollectionView
  6. 第一百三十一期:2019年容器使用报告:Docker 和 Kubernetes 王者地位不倒!
  7. RFC:Request For Comments
  8. STL--deque用法
  9. 用python打印心形_Python和Js打印心形
  10. OpenCV-证件照蓝底换成白底(或其他颜色如红色)
  11. 《七哥说道》第十二章:稍息,窗外的都是兄弟
  12. [转]尼康D7000或D7100优化校准与白平衡设置摄影指南
  13. 激光雕刻机装上AI,混合材料T恤上都能雕出花,自动变换力度保证不割破
  14. 【2019年03月29日】股票的滚动市盈率PE最低排名
  15. 最新:GoDadddy注册的域名,如何将域名解析设置到腾讯邮箱(MX记录)
  16. php 利用 PHPExcel 导出 导入 Excel 方法介绍(功能介绍)
  17. 在Linux下完美运行Windows PC版QQ/TIM教程
  18. Spring 事务管理
  19. 题解 CF940A 【Points on the line】
  20. 最全软件测试工具大全

热门文章

  1. STL源码剖析(三)
  2. springcloud- FeginClient 调用统一拦截添加请求头 RequestInterceptor ,被调用服务获取请求头...
  3. 20172311 2017-2018-2 《程序设计与数据结构》实验一报告
  4. Kafka.net使用编程入门
  5. Android AES加密算法及其实现
  6. SQL Server 触发器--备忘
  7. liferay+portlet+开发实例
  8. 如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
  9. ValueError: I/O operation on closed file 解决办法
  10. C语言之数组和指针位移的本质(四十五)