【发布时间】:2022-12-31 22:35:21
【问题描述】:
要温柔。我是一名高中校长,兼职为我们的学校网站编写代码。
我查看了答案,here、here 和here。我可能只是不够了解,无法提出正确的问题。
我们的活动有多个会议,并且有可以与多个活动中的多个会议相关联的研讨会。
我正在尝试获取一个 csv 结果,稍后将其放入一个数组中,用于我的研讨会的相关会话和事件。
下面的查询在没有第二个嵌套 Select 语句的情况下工作。
在 Alt_Events 语句中,我需要提取与从第一个嵌套 Select 中提取的 Session_ID 关联的 Event_ID。
Events
ID | Name | Description
1 | Flex Learning | A day of flexible learning.
2 | Moonshot Expo | A day to join partners to solve problems.
Event_Sessions
ID | Event_ID | Name | Description
1 | 1 | Morning Session | The first session of the day.
2 | 1 | Afternoon Session | The afternoon session.
3 | 1 | Tutoring Session | A chance to get help from teachers.
4 | 2 | Partner Field Trip | The first session of the day.
5 | 2 | Brainstorming Session | The afternoon session.
6 | 2 | Tutoring Session | A chance to get help from teachers.
Event_Workshops
ID | Name | Description
1 | Math Tutorial | Get help from your math teachers.
Event_Workshop_Links
ID | Workshop_ID | Session_ID
1 | 1 | 3
2 | 1 | 6
Output Table:
ID | Name | Description | ... | Alt_Sessions | Alt_Events
1 | Math Tutorial | Get help... | ... | 3,6 | 1,2
这是我的查询。
SELECT
ws.ID, ws.Name, ws.Description, ws.Location, ws.Owner_ID, ws.Max_Attendees,
ws.Eng_Major_Allowed, ws.Eng_Minor_Allowed,
ws.HC_Major_Allowed, ws.HC_Minor_Allowed,
ws.IT_Major_Allowed, ws.IT_Minor_Allowed,
u.LastName as Owner_LastName, u.FirstName AS Owner_FirstName, u.Email AS Owner_Email,
(SELECT group_concat(SESSION_ID) FROM Events_Workshops_Links WHERE Workshop_ID = ws.ID) AS Alt_Sessions,
(SELECT group_concat(Event_ID) FROM Event_Sessions WHERE Session_ID IN Alt_Sessions) AS Alt_Events
FROM Event_Workshops as ws
LEFT JOIN users AS u
ON ws.Owner_ID = u.ID
WHERE ws.ID = ?
ORDER BY ws.Name
我需要能够提取 Alt_Sessions 结果中的所有 event_id。
我猜我不能在第二个嵌套查询中使用第一个嵌套查询的结果。如果那是问题所在,我该如何提取事件 ID 列表?
非常感谢任何帮助。
(已更新以显示预期输出。转录查询时还有一个错误。第二个嵌套语句中的 Session_ID 而不是 Event_ID。
【问题讨论】:
-
您的子查询首先执行,因此您不能在第二个 SELECT 中使用 Alt_Sessions。您需要为 Alt_sessions 和 Alt_Events 执行子查询,然后在您的查询中使用此信息。
-
你能用预期的输出表更新你的帖子吗?
-
@lemon - 刚刚更新。
标签: mysql