mysql中json_remove函数的使用?

需求描述:

  今天看json记录,可以通过json_remove函数对一个key或多个key从个json记录中去掉.

操作过程:

1.查看一个已经存在的json表

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)

2.通过json_remove函数,将json记录中的key去掉

mysql> select json_remove(data,'$.age') from tab_json;  #查询时,去掉某些的key
+--------------------------------------------------------------------------+
| json_remove(data,'$.age')                                                |
+--------------------------------------------------------------------------+
| {"tel": 15046464563, "passcode": "654567"}                               |
| {"tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"} |
+--------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select json_remove(data,'$.age','$.name') from tab_json; #查询时,去掉某些的key
+---------------------------------------------------------+
| json_remove(data,'$.age','$.name')                      |
+---------------------------------------------------------+
| {"tel": 15046464563, "passcode": "654567"}              |
| {"tel": 189776542, "olds": "12", "address": "Hangzhou"} |
+---------------------------------------------------------+
2 rows in set (0.00 sec)

通过update将json记录中的值进行替换

mysql> update tab_json set data = json_remove(data,'$.age'); #通过update已经将json记录进行了修改
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from tab_json;
+----+--------------------------------------------------------------------------+
| id | data                                                                     |
+----+--------------------------------------------------------------------------+
|  1 | {"tel": 15046464563, "passcode": "654567"}                               |
|  2 | {"tel": 189776542, "name": "David", "olds": "12", "address": "Hangzhou"} |
+----+--------------------------------------------------------------------------+
2 rows in set (0.00 sec)

备注:json_remove函数的作用就是查询时,将其中的某些key不显示,如果要更新源表,需要使用update操作.

 

文档创建时间:2018年6月6日10:08:09

转载于:https://www.cnblogs.com/chuanzhang053/p/9143644.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值