MySQL5.7中的JSON基本操作指南
前言
因为项目需要,存储字段存储成了json格式,在项目中是将查询出来的值通过jackson转成相应的bean进行处理的,觉得不够简单方便。
mysql从5.7版本开始就支持json格式的数据,操作用起来挺方便的。
建表
在新建表时字段类型可以直接设置为json类型,比如我们创建一张表:
mysql> create table `test_user`(`id` int primary key auto_increment, `name` varchar(50) not null, `info` json);
json类型字段可以为null
插入数据:
mysql> insert into test_user(`name`, `info`) values('xiaoming','{"sex": 1, "age": 18, "nick_name": "小萌"}');
json类型的字段必须时一个有效的json字符串
可以使用json_object()
函数构造json对象:
mysql> insert into test_user(`name`, `info`) values('xiaohua', json_object("sex", 0, "age", 17));
使用json_array()
函数构造json数组:
mysql> insert into test_user(`name`, `info`) values('xiaozhang', json_object("sex", 1, "age", 19, "tag", json_array(3,5,90)));
现在查看test_user表中的数据:
mysql> select * from test_user; +----+-----------+--------------------------------------------+ | id | name | info | +----+-----------+--------------------------------------------+ | 1 | xiaoming | {"age": 18, "sex": 1, "nick_name": "小萌"} | | 2 | xiaohua | {"age": 17, "sex": 0} | | 3 | xiaozhang | {"age": 19, "sex": 1, "tag": [3, 5, 90]} | +----+-----------+--------------------------------------------+ 3 rows in set (0.04 sec)
查询
表达式: 对象为json列->'$.键', 数组为json列->'$.键[index]'
mysql> select name, info->'$.nick_name', info->'$.sex', info->'$.tag[0]' from test_user; +-----------+---------------------+---------------+------------------+ | name | info->'$.nick_name' | info->'$.sex' | info->'$.tag[0]' | +-----------+---------------------+---------------+------------------+ | xiaoming | "小萌" | 1 | null | | xiaohua | null | 0 | null | | xiaozhang | null | 1 | 3 | +-----------+---------------------+---------------+------------------+ 3 rows in set (0.04 sec)
等价于:对象为json_extract(json列 , '$.键')
,数组为json_extract(json列 , '$.键[index]')
mysql> select name, json_extract(info, '$.nick_name'), json_extract(info, '$.sex'), json_extract(info, '$.tag[0]') from test_user; +-----------+-----------------------------------+-----------------------------+--------------------------------+ | name | json_extract(info, '$.nick_name') | json_extract(info, '$.sex') | json_extract(info, '$.tag[0]') | +-----------+-----------------------------------+-----------------------------+--------------------------------+ | xiaoming | "小萌" | 1 | null | | xiaohua | null | 0 | null | | xiaozhang | null | 1 | 3 | +-----------+-----------------------------------+-----------------------------+--------------------------------+ 3 rows in set (0.04 sec)
不过看到上面"小萌"是带双引号的,这不是我们想要的,可以用json_unquote函数将双引号去掉
mysql> select name, json_unquote(info->'$.nick_name') from test_user where name='xiaoming'; +----------+-----------------------------------+ | name | json_unquote(info->'$.nick_name') | +----------+-----------------------------------+ | xiaoming | 小萌 | +----------+-----------------------------------+ 1 row in set (0.05 sec)
也可以直接使用操作符->>
mysql> select name, info->>'$.nick_name' from test_user where name='xiaoming'; +----------+----------------------+ | name | info->>'$.nick_name' | +----------+----------------------+ | xiaoming | 小萌 | +----------+----------------------+ 1 row in set (0.06 sec)
当然属性也可以作为查询条件
mysql> select name, info->>'$.nick_name' from test_user where info->'$.nick_name'='小萌'; +----------+----------------------+ | name | info->>'$.nick_name' | +----------+----------------------+ | xiaoming | 小萌 | +----------+----------------------+ 1 row in set (0.05 sec)
值得一提的是,可以通过虚拟列对json类型的指定属性进行快速查询。
创建虚拟列:
mysql> alter table `test_user` add `nick_name` varchar(50) generated always as (info->>'$.nick_name') virtual;
注意用操作符->>
使用时和普通类型的列查询是一样:
mysql> select name,nick_name from test_user where nick_name='小萌'; +----------+-----------+ | name | nick_name | +----------+-----------+ | xiaoming | 小萌 | +----------+-----------+ 1 row in set (0.05 sec)
更新
使用json_insert()
插入新值,但不会覆盖已经存在的值
mysql> update test_user set info = json_insert(info, '$.sex', 1, '$.nick_name', '小花') where id=2;
看下结果
mysql> select * from test_user where id=2; +----+---------+--------------------------------------------+-----------+ | id | name | info | nick_name | +----+---------+--------------------------------------------+-----------+ | 2 | xiaohua | {"age": 17, "sex": 0, "nick_name": "小花"} | 小花 | +----+---------+--------------------------------------------+-----------+ 1 row in set (0.06 sec)
使用json_set()
插入新值,并覆盖已经存在的值
mysql> update test_user set info = json_insert(info, '$.sex', 0, '$.nick_name', '小张') where id=3;
看下结果
mysql> select * from test_user where id=3; +----+-----------+---------------------------------------------------------------+-----------+ | id | name | info | nick_name | +----+-----------+---------------------------------------------------------------+-----------+ | 3 | xiaozhang | {"age": 19, "sex": 1, "tag": [3, 5, 90], "nick_name": "小张"} | 小张 | +----+-----------+---------------------------------------------------------------+-----------+ 1 row in set (0.06 sec)
使用json_replace()
只替换存在的值
mysql> update test_user set info = json_replace(info, '$.sex', 1, '$.tag', '[1,2,3]') where id=2;
看下结果
mysql> select * from test_user where id=2; +----+---------+--------------------------------------------+-----------+ | id | name | info | nick_name | +----+---------+--------------------------------------------+-----------+ | 2 | xiaohua | {"age": 17, "sex": 1, "nick_name": "小花"} | 小花 | +----+---------+--------------------------------------------+-----------+ 1 row in set (0.06 sec)
可以看到tag没有更新进去
删除
使用json_remove()
删除json元素
mysql> update test_user set info = json_remove(info, '$.sex', '$.tag') where id=1;
看下结果
mysql> select * from test_user where id=1; +----+----------+----------------------------------+-----------+ | id | name | info | nick_name | +----+----------+----------------------------------+-----------+ | 1 | xiaoming | {"age": 18, "nick_name": "小萌"} | 小萌 | +----+----------+----------------------------------+-----------+ 1 row in set (0.05 sec)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
上一篇: 馒头的热量高吗?吃馒头会不会长胖?
下一篇: 菠萝的营养价值,菠萝可以怎么吃