标签:color smi jin merge mys 创建 一个 -- 0.00
需求描述:
通过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
标签:color smi jin merge mys 创建 一个 -- 0.00
原文地址:https://www.cnblogs.com/chuanzhang053/p/9146466.html