【发布时间】:2021-09-07 05:53:15
【问题描述】:
我这几天一直在研究这个问题,只是无法完成最后一块拼图......
问题好像是得到一个嵌套对象,同层有2个嵌套对象?
我需要得到如下输出:
{
"Id": "19115",
"ClassScheduleId": "2143",
"MaxCapacity": "30",
"WebCapacity": "20",
"TotalBooked": "0",
"TotalBookedWaitlist": "0",
"WebBooked": "0",
"SemesterId": null,
"IsCanceled": "0",
"Substitute": "0",
"Active": "1",
"IsWaitlistAvailable": "0",
"IsEnrolled": "0",
"HideCancel": "0",
"IsAvailable": "0",
"StartDateTime": "2021-09-06T09:15:00+02:00",
"EndDateTime": "2021-09-06T10:15:00+02:00",
"LastModifiedDateTime": "2021-09-07T02:35:21+02:00",
"StaffId": "100000237",
"BookingStatus": "PaymentRequired",
"VirtualStreamLink": null,
"ClassDescription": {
"Id": "257",
"Active": "0",
"Description": "<div>Designed to burn calories while toning and lengthening, this challenging class mixes intense, high-impact cardio and plyometric training with full body conditioning and yoga. Using popular and motivating music, this fast-paced, high energy class will help create that long and lean look. No class is ever the same to ensure progressive results</div>",
"LastUpdated": "2021-08-17T15:31:21+02:00",
"Name": "Bottoms Up",
"Notes": "",
"Prereq": "",
"Program": {
"Id": "26",
"Name": "Classes",
"ScheduleType": "Class",
"CancelOffset": "0"
},
"SessionType": {
"Id": "207",
"Type": "All",
"Name": "Spinning",
"NumDeducted": "1",
"ProgramId": "26"
}
}
}
我最后一次尝试,我认为是“关闭”,但它不起作用并引发错误:-(
SELECT json_arrayagg(
json_object(
'ClassScheduleId', c.ClassScheduleId,
'MaxCapacity', c.MaxCapacity,
'WebCapacity', c.WebCapacity,
'TotalBooked', c.TotalBooked,
'TotalBookedWaitlist', c.TotalBookedWaitlist,
'WebBooked', c.WebBooked,
'IsCanceled', c.IsCanceled,
'Substitute', c.Substitute,
'Active', c.Active,
'IsWaitlistAvailable', c.IsWaitlistAvailable,
'IsEnrolled', c.IsEnrolled,
'HideCancel', c.HideCancel,
'Id', c.Id,
'IsAvailable', c.IsAvailable,
'StartDateTime', c.StartDateTime,
'EndDateTime', c.EndDateTime,
'LastModifiedDateTime', c.LastModifiedDateTime,
'BookingStatus', c.BookingStatus
'ClassDescription', json_object(
'Active', cd.Active,
'Description', cd.Description,
'Id', cd.Id,
'LastUpdated', cd.LastUpdated,
'Name', cd.Name,
'Notes', cd.Notes,
'Prereq', cd.Prereq,
GROUP_CONCAT(
'Program', json_object(
'Id', p.Id,
'Name', p.Name,
'ScheduleType', p.ScheduleType,
'CancelOffset', p.CancelOffset
),
'SessionType', json_object(
'Type', st.Type,
'Id', st.Id,
'Name', st.Name,
'NumDeducted', st.NumDeducted,
'ProgramId', st.ProgramId
)
)
)
)
)
FROM Classes as c
LEFT JOIN ClassDescription as cd ON cd.Id = c.ClassDescriptionId
LEFT JOIN Program as p ON p.Id = cd.ProgramId
LEFT JOIN SessionType as st ON st.Id = cd.SessionTypeId
有什么想法可以让我获得更好的文档或能够格式化/构建这些类型的查询的 SQL 可视化编辑器?
提前感谢您的任何意见!
【问题讨论】:
-
你试过 JSON_PRETTY 原生函数了吗?文档在这里:dev.mysql.com/doc/refman/8.0/en/json-utility-functions.html
-
您好,我确实检查了 JSON_PRETTY,但它似乎更适用于非常基本的输出,并且当您想要构建复杂的 JSON 对象时会变得“丑陋”。我上面的示例是最终 JSON OBJECT 的 30%,因此使用 json_pretty 会变得混乱。因此,在我的示例中,除了在“ClassDescription”中获取“Program”和“SessionType”元素之外,我还可以完成所有工作