mysql資料庫中,如何對json資料類型的值進行修改?通過json_set函數對json欄位值進行修改?

來源:互聯網
上載者:User

標籤:auto   address   修改   select   span   key   values   cte   beijing   

需求描述:

  今天在看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: 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欄位的索引值進行修改,如果存在就進行替換,如果不存在索引值,就增加索引值對.

 

文檔建立時間:2018年6月5日21:59:18

mysql資料庫中,如何對json資料類型的值進行修改?通過json_set函數對json欄位值進行修改?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.