日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql替换json的key_mysql中json_replace函数的使用?通过json_replace对json对象的值进行替换...

發布時間:2024/7/23 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql替换json的key_mysql中json_replace函数的使用?通过json_replace对json对象的值进行替换... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求描述:

在看mysql中關于json的內容,通過json_replace函數可以實現對json值的替換,

在此記錄下.

操作過程:

1.查看帶有json數據類型的表

mysql> select * from tab_json;

+----+---------------------------------------------------------------------------------------+

| id | data |

+----+---------------------------------------------------------------------------------------+

| 1 | {"age": "33", "tel": 13249872314, "passcode": "654567"} |

| 2 | {"age": "33", "tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"} |

+----+---------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

2.使用json_replace函數對json值進行操作

mysql> select json_replace(data,'$.age',54,'$.tel',15046464563) from tab_json where id = 1; #使用json_replace進行查詢處理,對已經存在的key值進行替換

+-------------------------------------------------------+

| json_replace(data,'$.age',54,'$.tel',15046464563) |

+-------------------------------------------------------+

| {"age": 54, "tel": 15046464563, "passcode": "654567"} |

+-------------------------------------------------------+

1 row in set (0.00 sec)

mysql> select json_replace(data,'$.age',54,'$.tel',15046464563,'$.sex',"male") from tab_json where id = 1; #對于不存在key,是沒有增加新的key-value值的

+------------------------------------------------------------------+

| json_replace(data,'$.age',54,'$.tel',15046464563,'$.sex',"male") |

+------------------------------------------------------------------+

| {"age": 54, "tel": 15046464563, "passcode": "654567"} |

+------------------------------------------------------------------+

1 row in set (0.00 sec)

3.通過update語句對json中的值進行替換操作

mysql> update tab_json set data = json_replace(data,'$.age',54,'$.tel',15046464563) where id = 1; #對id=1的行進行更新操作,更新之后,age和tel的值發生了變化

Query OK, 1 row affected (0.10 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from tab_json;

+----+---------------------------------------------------------------------------------------+

| id | data |

+----+---------------------------------------------------------------------------------------+

| 1 | {"age": 54, "tel": 15046464563, "passcode": "654567"} |

| 2 | {"age": "33", "tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"} |

+----+---------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

mysql> update tab_json set data = json_replace(data,'$.age',54,'$.tel',15046464563,'$.sex',"male") where id = 1; 對id=1的行進行更新操作,更新之后,age和tel的值發生了變化,但是并沒有增加新的key

Query OK, 0 rows affected (0.00 sec)

Rows matched: 1 Changed: 0 Warnings: 0

mysql> select * from tab_json;

+----+---------------------------------------------------------------------------------------+

| id | data |

+----+---------------------------------------------------------------------------------------+

| 1 | {"age": 54, "tel": 15046464563, "passcode": "654567"} |

| 2 | {"age": "33", "tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"} |

+----+---------------------------------------------------------------------------------------+

2 rows in set (0.00 sec)

備注:所以json_replace的主要作用是替換,如果存在key就替換對應的值,如果不存在key也不會增加,與json_insert的使用有區別.

文檔創建時間:2018年6月6日09:48:10

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的mysql替换json的key_mysql中json_replace函数的使用?通过json_replace对json对象的值进行替换...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。