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

SELECT REPLACE(json_extract(param,'$.payFundAcc'),'"','') from t_external_trade_event where status != 'S' ;

 

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"} |
+----+--------------------------------------------------------------+
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为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"} |
+----+-------------------------------------------------------------+
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"} |
+----+-----------------------------------------------------------------------------------+
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)

posted @ 2019-05-30 12:11  Struts-pring  阅读(1062)  评论(0编辑  收藏  举报