mysql數據庫中,如何對json數據類型的值進行修改?經過json_set函數對json字段值進行修改?

https://www.cnblogs.com/chuanzhang053/p/9142180.htmlhtml

需求描述:mysql

  今天在看mysql中存放json數據類型的問題,對於json數據進行修改的操做,sql

  在此記錄下.json

操做過程:函數

1.建立包含json數據類型的表,插入基礎數據code

複製代碼

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字段的值htm

複製代碼

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: 0

mysql> 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: 0

mysql> 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: 0

mysql> 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: 0

mysql> 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: 0

mysql> 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: 0

mysql> 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字段的鍵值進行修改,若是存在就進行替換,若是不存在鍵值,就增長鍵值對.blog