By json_set function to modify the value of the data field

mysql> update tab_json set data = json_set (data, ". $ address", "Guangzhou") where id = 1; # key on the address line id = 1 is modified.
Query the OK, Row 1 affected (0.26 sec )
the Rows Matched: the Changed. 1: Warnings. 1: 0

mysql> select * from tab_json;
+----+--------------------------------------------------------------+
| id | data |
+----+--------------------------------------------------------------+
| 1 | {"tel": 13249872314, "name": "Mike", "address": "Guangzhou"} |
| 2 | {"tel": 189776542, "name": "David", "address": "Shanghai"} |
+----+--------------------------------------------------------------+
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"} |
+----+-------------------------------------------------------------+
rows in set (0.00 sec)

mysql> update tab_json set data = json_set (data, "$ address.", "Hangzhou") where id = 2; # id to be modified to address the key 2
Query the OK,. 1 Row affected (0.03 sec)
the 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"} |
+----+-------------------------------------------------------------+
rows in set (0.00 sec)

mysql> update tab_json set data = json_set (data, "$ passcode.", "654567") where id = 1; # passcode to modify the id field is 1, this key is not found, it adds a key pair .
Query the OK, 1 Row affected (0.00 sec)
the 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"} |
+----+-----------------------------------------------------------------------------------+
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"} |
+----+-----------------------------------------------------------------------------------+
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"} |
+----+------------------------------------------------------------------------------------------------+
rows in set (0.00 sec)

Guess you like

Origin www.cnblogs.com/Struts-pring/p/10948870.html