首页 技术 正文
技术 2022年11月9日
0 收藏 680 点赞 3,271 浏览 4838 个字

需求描述:

  今天在看mysql中存放json数据类型的问题,对于json数据进行修改的操作,

  在此记录下.

操作过程:

1.创建包含json数据类型的表,插入基础数据

mysql> create table tab_json(id int not null auto_increment primary key,data json);
Query OK, 0 rows affected (0.03 sec)mysql> insert into tab_json values (null,'{"name":"Mike","address":"Beijing","tel":13249872314}');
Query OK, 1 row affected (0.01 sec)mysql> insert into tab_json values (null,'{"name":"David","address":"Shanghai","tel":189776542}');
Query OK, 1 row affected (0.01 sec)mysql> select * from tab_json;
+----+------------------------------------------------------------+
| id | data |
+----+------------------------------------------------------------+
| 1 | {"tel": 13249872314, "name": "Mike", "address": "Beijing"} |
| 2 | {"tel": 189776542, "name": "David", "address": "Shanghai"} |
+----+------------------------------------------------------------+
2 rows in set (0.00 sec)

2.通过json_set函数,来修改data字段的值

mysql> update tab_json set data = json_set(data,"$.address","Guangzhou") where id = 1;  #对id = 1的行的address的键值进行修改.
Query OK, 1 row affected (0.26 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from tab_json;
+----+--------------------------------------------------------------+
| id | data                                                         |
+----+--------------------------------------------------------------+
|  1 | {"tel": 13249872314, "name": "Mike", "address": "Guangzhou"} |
|  2 | {"tel": 189776542, "name": "David", "address": "Shanghai"}   |
+----+--------------------------------------------------------------+
2 rows in set (0.00 sec)mysql> update tab_json set data = json_set(data,"$.address","Shenzhen");
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0mysql> select * from tab_json;
+----+-------------------------------------------------------------+
| id | data                                                        |
+----+-------------------------------------------------------------+
|  1 | {"tel": 13249872314, "name": "Mike", "address": "Shenzhen"} |
|  2 | {"tel": 189776542, "name": "David", "address": "Shenzhen"}  |
+----+-------------------------------------------------------------+
2 rows in set (0.00 sec)mysql> update tab_json set data = json_set(data,"$.address","Hangzhou") where id = 2; #对id为2的address键值进行修改
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from tab_json;
+----+-------------------------------------------------------------+
| id | data                                                        |
+----+-------------------------------------------------------------+
|  1 | {"tel": 13249872314, "name": "Mike", "address": "Shenzhen"} |
|  2 | {"tel": 189776542, "name": "David", "address": "Hangzhou"}  |
+----+-------------------------------------------------------------+
2 rows in set (0.00 sec)mysql> update tab_json set data = json_set(data,"$.passcode","654567") where id = 1; #对id为1的passcode字段进行修改,发现没有这个键值,就增加了一个键值对.
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from tab_json;
+----+-----------------------------------------------------------------------------------+
| id | data                                                                              |
+----+-----------------------------------------------------------------------------------+
|  1 | {"tel": 13249872314, "name": "Mike", "address": "Shenzhen", "passcode": "654567"} |
|  2 | {"tel": 189776542, "name": "David", "address": "Hangzhou"}                        |
+----+-----------------------------------------------------------------------------------+
2 rows in set (0.00 sec)mysql> update tab_json set data = json_set(data,"$.olds","12") where id = 2;
Query OK, 1 row affected (0.17 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from tab_json;
+----+-----------------------------------------------------------------------------------+
| id | data                                                                              |
+----+-----------------------------------------------------------------------------------+
|  1 | {"tel": 13249872314, "name": "Mike", "address": "Shenzhen", "passcode": "654567"} |
|  2 | {"tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"}          |
+----+-----------------------------------------------------------------------------------+
2 rows in set (0.00 sec)mysql> update tab_json set data = json_set(data,"$.age","33");
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0mysql> select * from tab_json;
+----+------------------------------------------------------------------------------------------------+
| id | data                                                                                           |
+----+------------------------------------------------------------------------------------------------+
|  1 | {"age": "33", "tel": 13249872314, "name": "Mike", "address": "Shenzhen", "passcode": "654567"} |
|  2 | {"age": "33", "tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"}          |
+----+------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

备注:以上就是通过json_set进行对json字段的键值进行修改,如果存在就进行替换,如果不存在键值,就增加键值对.

文档创建时间:2018年6月5日21:59:18

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893