简体   繁体   English

如何在 SQL 查询中使用 JSON_ARRAYAGG 和 JSON_OBJECT 获取一个数组中的数据?

[英]How can i get data in one array using JSON_ARRAYAGG and JSON_OBJECT in SQL query?

This is my query, i'm using JSON_ARRAYAGG and JSON_OBJECT for hierarchy relations, but my concern is that i want data in following formated data.这是我的查询,我使用 JSON_ARRAYAGG 和 JSON_OBJECT 来处理层次关系,但我担心的是我想要以下格式化数据中的数据。 Don't be confuse with joins just help me get the data i want.不要与连接混淆,只是帮助我获取我想要的数据。

SELECT 
  CU.contact_group_id, 
  JSON_ARRAYAGG(
    JSON_OBJECT('contact_user', users.user)
  ) AS `contact_users` 
FROM 
  contact_user as CU 
  INNER JOIN (
    SELECT 
      U.id, 
      JSON_ARRAYAGG(
        JSON_OBJECT('id', U.id, 'user', U.first_name)
      ) as `user` 
    FROM 
      users as U 
) as `users` on CU.user_id = users.id 

Data i get:我得到的数据:

                "users": [
                            {
                                "contact_user": [
                                    {
                                        "id": 1,
                                        "user": "dash"
                                    },
                                    {
                                        "id": 3,
                                        "user": "dash1"
                                    }
                                ]
                            }
                        ]

Data i want:我想要的数据:

                        "users": [
                                    {
                                        "id": 1,
                                        "user": "dash"
                                    },
                                    {
                                        "id": 3,
                                        "user": "dash1"
                                    }
                                ]
                            

Test this:测试这个:

SELECT JSON_OBJECT('users', JSON_ARRAYAGG(JSON_OBJECT('id', users.id, 'user', users.first_name))) AS `users` 
FROM users
-- check if the following line is really needed
-- INNER JOIN contact_user on contact_user.user_id = users.id 
SELECT 
  CU.contact_group_id, 
  users.user AS `contact_users` 
FROM 
  contact_user as CU 
  INNER JOIN (
    SELECT 
      U.id, 
      JSON_ARRAYAGG(
        JSON_OBJECT('id', U.id, 'user', U.first_name)
      ) as `user` 
    FROM 
      users as U 
) as `users` on CU.user_id = users.id 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 JSON_OBJECT 时如何在 JSON_ARRAYAGG 中返回 DISTINCT 值 - How to return DISTINCT values in a JSON_ARRAYAGG when using JSON_OBJECT JSON_ARRAYAGG 中的 JSON_OBJECT 有时返回 json 数组,有时返回字符串 - JSON_OBJECT inside JSON_ARRAYAGG sometimes return json array, sometimes string 当我在 MySQL 中使用带有内部连接的 `json_arrayagg` 和 `json_object` 时,为什么结果重复? - When i use `json_arrayagg` & `json_object` with inner join in MySQL, Why are the results repeated? 使用 json_arrayagg 重复数据 - Data get repeated with json_arrayagg 在 v5.6 或 PHP 中替换 MySQL 中的 JSON_ARRAYAGG 和 JSON_OBJECT - Replacement for JSON_ARRAYAGG and JSON_OBJECT from MySQL in v5.6 or PHP 如何对 Mysql 中的 json_arrayagg() 返回的数组进行排序? - How is it possible to order the array returned by json_arrayagg() in Mysql? MySQL 在 SELECT IN 子句中使用 JSON_ARRAYAGG? - MySQL using JSON_ARRAYAGG in a SELECT IN clause? 如何控制生成的 JSON 字符串中的 JSON_ARRAYAGG 元素顺序 - How to control the JSON_ARRAYAGG elements order in the resulting JSON string 使用 INNER JOIN 在 JSON_ARRAYAGG 上删除 MySQL JOIN 查询上的重复项 - Remove duplicates on MySQL JOIN query on JSON_ARRAYAGG with INNER JOIN 为什么在 SELECT 使用 JSON_ARRAYAGG 时忽略 LIMIT? - Why the LIMIT are ignored when SELECT using JSON_ARRAYAGG?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM