zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    VS提示“项目文件" "已被重命名或已不在解决方案中”的解决办法 .
    微信公众平台教程和SDK收集
    “SQLServerAgent当前未运行”问题解决
    $(document).click() 在苹果手机上不能正常运行
    友盟iOS推送配置(从真机调试到推送)
    Ubuntu安装VMware Tools的方法
    TortoiseSVN客户端如何更改新的URL
    Windows Server 2008系统如何取消登录时要按Ctrl+Alt+Delete组合键
    Windows Server 2008 显示桌面图标
    用WinRAR进行安装包的制作
  • 原文地址:https://www.cnblogs.com/chuanzhang053/p/9143644.html
Copyright © 2011-2022 走看看