Collections:
JSON_OBJECTAGG() - Building JSON Object in Group
How to build a JSON object with 2 field expressions in result set groups using the JSON_OBJECTAGG() function?
✍: FYIcenter.com
JSON_OBJECTAGG(key, value) is a MySQL built-in aggregate function that
creates a JSON object with 2 field expressions in result set groups.
For example:
SELECT help_category_id, LEFT(JSON_OBJECTAGG(help_topic_id, name), 40)
FROM mysql.help_topic GROUP BY help_category_id;
-- +------------------+-----------------------------------------------+
-- | help_category_id | LEFT(JSON_OBJECTAGG(help_topic_id, name), 40) |
-- +------------------+-----------------------------------------------+
-- | 1 | {"0": "HELP_DATE", "1": "HELP_VERSION"} |
-- | 2 | {"2": "AUTO_INCREMENT", "6": "BIT", "7":... |
-- | 3 | {"3": "HELP COMMAND", "618": "SET", "619... |
-- | 4 | {"4": "TRUE", "5": "FALSE"} |
-- | 5 | {"40": "GEOMETRY HIERARCHY", "43": "SPAT... |
-- ...
-- +------------------+-----------------------------------------------+
SELECT help_category_id, help_topic_id, name
FROM mysql.help_topic WHERE help_category_id = 5;
-- +------------------+---------------+--------------------+
-- | help_category_id | help_topic_id | name |
-- +------------------+---------------+--------------------+
-- | 5 | 40 | GEOMETRY HIERARCHY |
-- | 5 | 43 | SPATIAL COLUMNS |
-- | 5 | 44 | SPATIAL INDEXES |
-- +------------------+---------------+--------------------+
JSON_OBJECTAGG() is also a window function, you can call it with the OVER clause to create a JSON object with 2 field expressions in the current window. For example:
SELECT help_topic_id AS tid, help_category_id AS cid,
LEFT((JSON_OBJECTAGG(help_topic_id, name) OVER w), 40)
FROM mysql.help_topic
WINDOW w AS (PARTITION BY help_category_id);
-- +-----+-----+--------------------------------------------------------+
-- | tid | cid | LEFT((JSON_OBJECTAGG(help_topic_id, name) OVER w), 40) |
-- +-----+-----+--------------------------------------------------------+
-- | 0 | 1 | {"0": "HELP_DATE", "1": "HELP_VERSION"} |
-- | 1 | 1 | {"0": "HELP_DATE", "1": "HELP_VERSION"} |
-- | 2 | 2 | {"2": "AUTO_INCREMENT", "6": "BIT", "7": |
-- | 6 | 2 | {"2": "AUTO_INCREMENT", "6": "BIT", "7": |
-- | 7 | 2 | {"2": "AUTO_INCREMENT", "6": "BIT", "7": |
-- | 8 | 2 | {"2": "AUTO_INCREMENT", "6": "BIT", "7": |
-- | 9 | 2 | {"2": "AUTO_INCREMENT", "6": "BIT", "7": |
-- ...
-- +-----+-----+--------------------------------------------------------+
Reference information of the JSON_OBJECTAGG() function:
JSON_OBJECTAGG(key, value): json Takes two column names or expressions as arguments, the first of these being used as a key and the second as a value, and returns a JSON object containing key-value pairs. Returns NULL if the result contains no rows, or in the event of an error. If duplicate keys exist in the result set group, only the last value encountered is used with that key in the JSON output (“last duplicate key wins”). Arguments, return value and availability: key: Required. The field expression used as keys in JSON object. value: Required. The field expression used as values in JSON object. json: Return value. The JSON object encoded as a string. Available since MySQL 5.7.
⇒ MAX() - Maximum Value in Group
⇐ JSON_ARRAYAGG() - Building JSON Array in Group
2023-11-18, 1752🔥, 0💬
Popular Posts:
How to obtain the version number of the ICU (International Components for Unicode) library using the...
How To List All Login Names on the Server in SQL Server? If you want to see a list of all login name...
Where to find answers to frequently asked questions on Downloading and Installing SQL Server 2005 Ex...
How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions in SQL Server Transact-...
How To Get the Definition of a View Out of the SQL Server in SQL Server? If you want get the definit...