0

将尽我所能在这里解释我的问题。我运行了一个查询,该查询适用于检查已完成任一courseIDNum=11courseIDNum=12in 的人districtIDNum=5,但不是同时完成 courseIDNums。这是脚本:

SELECT max(p.FirstName),
   max(p.LastName),
   max(p.Email),
   max(s.CourseIDNum)
FROM People p
INNER JOIN Registration r
   on p.PeopleID = r.PeopleIdNum
INNER JOIN Section s
   on r.SectionIDNum = s.SectionID
INNER JOIN School sc
   on p.SchoolIDNum = sc.SchoolID
WHERE s.CourseIDNum IN (11, 12)
AND sc.DistrictIDNum = 5
AND r.Completed='Y'
group by p.PeopleID
having count(distinct s.CourseIDNum)=1

现在我的任务是districtIDNum=5再次为人们检索数据,但这次是检查他们是否已经参加了所有 3 门课程,3 门课程中有 2 门具有相同的课程名称,但这 2 门课程中的每门都有 2 个不同的 courseIDNums。所以,我编辑了WHERE如下所示的部分。返回的结果只有完成12和的人,177没有和的结果。然后,我将OR替换为AND运算符,返回0 行。非常感谢您对此的任何帮助!1168128

WHERE s.CourseIDNum IN (11, 12)
   OR s.CourseIDNum IN (68, 177)
   OR s.CourseIDNum=128
   AND sc.DistrictIDNum = 5
   AND r.Completed='Y'
group by p.PeopleID
having count(distinct s.CourseIDNum)=3

以下是涉及的表格:

peopleID FirstName LastName Email schoolIDNum
1 Esther B b@hotmail.com 33
2 Tommy L l@hotmail.com 55
3 Liz M m@hotmail.com 90

注册ID peopleIDNum sectionIDNum
22 1 40
23 2 41
24 3 132
25 1 78
26 2 52
27 1 63

sectionID courseIDNum
40 11
41 12
52 68
63 128
78 177
132 195

courseID coursename
11 Health(旧)
12 Health(新)
68 PE(旧)
128 键盘输入
177 PE(新)
195 Computing

schoolID 地区IDNum
33 5
55 5
90 12

4

0 回答 0