我正在创建一个数据库,学生可以在其中注册研讨会,并且我有一个保存 StudentID 和 WorkshopID 的表 WorkshopRegistration。我的目标是返回一个包含 StudentName、SchoolName、Workshop1、Workshop2 列的表。学生将永远不会参加超过 2 个研讨会。我已经到了将 1 个研讨会作为专栏的地步,但我在获得第二个研讨会时遇到了麻烦。
SELECT Students.StudentID, Students.name, Schools.name,
(SELECT Workshops.title
FROM Workshops
WHERE Workshops.WorkshopID IN
(SELECT WorkshopID
FROM WorkshopRegistration
WHERE WorkshopRegistration.StudentID=Students.StudentID)) as Workshop1,
(SELECT Workshops.title
FROM Workshops
WHERE Workshops.WorkshopID IN
(SELECT WorkshopID
FROM WorkshopRegistration
WHERE WorkshopRegistration.StudentID=Students.StudentID)) as Workshop2
FROM Students, Schools
WHERE Students.SchoolID=Schools.SchoolID;