• mysql中json_extract函数的使用?作用是什么?


    需求描述:

      今天看mysql中的json数据类型,涉及到一些使用,使用到了函数json_extract来

      获取json字段中某个key的值,在此记录下.

    操作过程:

    1.查看包含json字段的表信息

    mysql> desc tab_json;
    +-------+------------+------+-----+---------+----------------+
    | Field | Type       | Null | Key | Default | Extra          |
    +-------+------------+------+-----+---------+----------------+
    | id    | bigint(20) | NO   | PRI | NULL    | auto_increment |
    | data  | json       | YES  |     | NULL    |                |
    +-------+------------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)

    mysql> show create table tab_json;
    +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table    | Create Table                                                                                                                                                                |
    +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | tab_json | CREATE TABLE `tab_json` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `data` json DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
    +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)

    mysql> select * from tab_json;
    +----+----------------------------------------------------------------+
    | id | data                                                           |
    +----+----------------------------------------------------------------+
    |  1 | {"Tel": "132223232444", "name": "david", "address": "Beijing"} |
    |  2 | {"Tel": "13390989765", "name": "Mike", "address": "Guangzhou"} |
    +----+----------------------------------------------------------------+
    2 rows in set (0.00 sec)
     

    备注:data字段就是json的数据类型,由键值对组成.

    2.json_extract函数的使用

     
    mysql> select json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel");
    +--------------------------------------------------------------+
    | json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.tel") |
    +--------------------------------------------------------------+
    | "13240133388"                                                |
    +--------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.name");
    +---------------------------------------------------------------+
    | json_extract('{"name":"Zhaim","tel":"13240133388"}',"$.name") |
    +---------------------------------------------------------------+
    | "Zhaim"                                                       |
    +---------------------------------------------------------------+
    1 row in set (0.00 sec)
     

    备注:这个查询时直接对json对象进行操作.

    3.对tab_json表使用json_extract函数

     
    mysql> select json_extract(data,'$.name') from tab_json;
    +-----------------------------+
    | json_extract(data,'$.name') |
    +-----------------------------+
    | "david"                     |
    | "Mike"                      |
    +-----------------------------+
    2 rows in set (0.00 sec)
    
    mysql> select json_extract(data,'$.name'),json_extract(data,'$.tel') from tab_json;  #如果查询没有的key,那么是可以查询,不过返回的是NULL.
    +-----------------------------+----------------------------+
    | json_extract(data,'$.name') | json_extract(data,'$.tel') |
    +-----------------------------+----------------------------+
    | "david"                     | NULL                       |
    | "Mike"                      | NULL                       |
    +-----------------------------+----------------------------+
    2 rows in set (0.00 sec)
    
    mysql> select json_extract(data,'$.name'),json_extract(data,'$.address') from tab_json;
    +-----------------------------+--------------------------------+
    | json_extract(data,'$.name') | json_extract(data,'$.address') |
    +-----------------------------+--------------------------------+
    | "david"                     | "Beijing"                      |
    | "Mike"                      | "Guangzhou"                    |
    +-----------------------------+--------------------------------+
    2 rows in set (0.00 sec)
     

    备注:通过json_extract函数,获取到了json对象的特定key的值.

  • 相关阅读:
    教了公司新来的小姐姐这七种排序算法以及实现,她一脸崇拜的看着我
    历时两年零三个月,从刚毕业的外包到现在的阿里P7offer,我只做了这几件事
    深度分享:面试阿里,字节跳动,美团90%会被问到的HashMap知识
    备份一下我的.bash_aliases文件
    Error(10028):Can't resolve multiple constant drivers for net “ ” at **.v
    Error (10170): Verilog HDL syntax error at passwd.v(21) near text "if"; expecting an identifier ("if" is a reserved keyword ), or "endmodule", or a parallel statement
    正在从 Windows 应用商店下载... 无法从 Windows 应用商店下载。请检查网络连接。
    完全卸载win10上的Ubuntu子系统
    >>> print "hello" SyntaxError: Missing parentheses in call to 'print'
    深入Linux内核架构
  • 原文地址:https://www.cnblogs.com/zhangzhiping35/p/10678393.html
Copyright © 2020-2023  润新知