• mysql中json_merge函数的使用?


    需求描述:

      通过mysql中的json_merge函数,可以将多个json对象合并成一个对象

    操作过程:

    1.查看一张包含json列的表

    mysql> select * from tab_json;
    +----+-----------------------------------------------------------------------------------+
    | id | data                                                                              |
    +----+-----------------------------------------------------------------------------------+
    |  1 | {"Tel": "132223232444", "name": "david", "address": "Beijing"}                    |
    |  2 | {"Tel": "13390989765", "name": "Mike", "address": "Guangzhou"}                    |
    |  3 | {"names": "Smith"}                                                                |
    |  4 | {"names": "Smith", "address": "Beijing"}                                          |
    |  5 | {"names": "Smith", "address": "Beijing", "birthday": "2018-09-09"}                |
    |  6 | {"Max": "true", "names": "Smith", "address": "Beijing", "birthday": "2018-09-09"} |
    |  7 | {"max": "true", "names": "Smith", "address": "Beijing", "birthday": "2018-09-09"} |
    |  8 | {"oax": "true", "names": "Smith", "address": "Beijing", "birthday": "2018-09-09"} |
    +----+-----------------------------------------------------------------------------------+
    8 rows in set (0.00 sec)

    2.将names的值与address的值进行合并

    mysql> select json_extract(data,'$.names'),json_extract(data,'$.address') from tab_json;
    +------------------------------+--------------------------------+
    | json_extract(data,'$.names') | json_extract(data,'$.address') |
    +------------------------------+--------------------------------+
    | NULL                         | "Beijing"                      |
    | NULL                         | "Guangzhou"                    |
    | "Smith"                      | NULL                           |
    | "Smith"                      | "Beijing"                      |
    | "Smith"                      | "Beijing"                      |
    | "Smith"                      | "Beijing"                      |
    | "Smith"                      | "Beijing"                      |
    | "Smith"                      | "Beijing"                      |
    +------------------------------+--------------------------------+
    8 rows in set (0.00 sec)
    
    mysql> select json_merge(json_extract(data,'$.names'),json_extract(data,'$.address')) from tab_json;
    +-------------------------------------------------------------------------+
    | json_merge(json_extract(data,'$.names'),json_extract(data,'$.address')) |
    +-------------------------------------------------------------------------+
    | NULL                                                                    |
    | NULL                                                                    |
    | NULL                                                                    |
    | ["Smith", "Beijing"]                                                    |
    | ["Smith", "Beijing"]                                                    |
    | ["Smith", "Beijing"]                                                    |
    | ["Smith", "Beijing"]                                                    |
    | ["Smith", "Beijing"]                                                    |
    +-------------------------------------------------------------------------+
    8 rows in set (0.00 sec)

    3.如果多个对象含有相同的key,那么也会进行合并为具体的values

    mysql> SELECT JSON_MERGE('{"a": 1, "b": 2}', '{"c": 3, "a": 4}');
    +----------------------------------------------------+
    | JSON_MERGE('{"a": 1, "b": 2}', '{"c": 3, "a": 4}') |
    +----------------------------------------------------+
    | {"a": [1, 4], "b": 2, "c": 3}                      |
    +----------------------------------------------------+
    1 row in set (0.00 sec)

    备注:将两个对象的值合并成一个,a这个key的值也增加到了2个.

    文档创建:2018年6月6日17:49:18

  • 相关阅读:
    Heavy Transportation POJ
    Frogger POJ
    CODEFORCES 25E Test
    POJ
    POJ-2777
    [ZJOI2008]骑士
    POJ
    POJ
    [USACO12FEB]Nearby Cows
    [HAOI2009]毛毛虫
  • 原文地址:https://www.cnblogs.com/chuanzhang053/p/9146466.html
Copyright © 2020-2023  润新知