What is the use of Json_merge functions in MySQL?

Source: Internet
Author: User

Requirements Description :

With the Json_merge function in MySQL, you can combine multiple JSON objects into one object

Operation Process :

1. View a table with JSON columns

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. Merges the value of names with the value of 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) m Ysql> SelectJson_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. If multiple objects contain the same key, they are also merged into a specific values

mysql> SELECT json_merge ('{"a": 1, "B": 2}', '"a": 4}[1, 4], "B": 2, "C": 3}                      |+----------------------------------------------------+1 row in Set (0.00 sec)

Note: Combining the values of two objects into one, the value of a key also increases to 2.

Document created: June 6, 2018 17:49:18

What is the use of Json_merge functions in MySQL?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.