[英]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.