码迷,mamicode.com
首页 > 数据库 > 详细

mysql中json_merge函数的使用?

时间:2018-06-06 21:50:02      阅读:192      评论:0      收藏:0      [点我收藏+]

标签: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

mysql中json_merge函数的使用?

标签:color   smi   jin   merge   mys   创建   一个   --   0.00   

原文地址:https://www.cnblogs.com/chuanzhang053/p/9146466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!