mysql中json_merge函数的使用?

Posted QA-3K

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于mysql中json_merge函数的使用?的主要内容,如果未能解决你的问题,请参考以下文章

关于dede后台登陆后一片空白

Android 逆向ART 脱壳 ( InMemoryDexClassLoader 脱壳 | BaseDexClassLoader 构造函数 | DexPathList 构造函数及后续调用 )(代码片

SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase(代码片

解决报错提示:Loading class `com.mysql.jdbc.Driver‘.The new driver class is ‘com.mysql.cj.jdbc.Driver‘.(代码片

Android 逆向整体加固脱壳 ( DexClassLoader 加载 dex 流程分析 | DexFile loadDexFile 函数 | 构造函数 | openDexFile 函数 )(代码片

Android 逆向Android 进程注入工具开发 ( 注入代码分析 | 远程调用 目标进程中 libc.so 动态库中的 mmap 函数 二 | 准备参数 | 远程调用 mmap 函数 )(代码片