使用“ AND ”运算符获取查询以检索所需结果时遇到困难。需要显示完成了 3 门课程中的任何一个的人的firstname
, lastname
,email
和courseID
in districtIDNum=5
,即(Health Old (courseIDNum=11) OR Health New (courseIDNum=12)) AND (PE Old (courseIDNum=68 OR PE New (courseIDNum) =177)) AND courseIDNum=128 . 请知道11
和12
实际上是同一门课程:一个是旧课程,另一个是新课程(与68
and相同177
),因此 DistrictIDNum=5 的人只能选择其中一个;这就是我尝试使用 (11, 12) 的原因。但是,它无法正常工作,因为它返回 0 行。
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 s.CourseIDNum IN (68, 177)
AND s.CourseIDNum=128
AND sc.DistrictIDNum = 5
AND r.Completed='Y'
group by p.PeopleID
having count(distinct s.CourseIDNum)=1
不应该返回的行Liz M
来自districtIDNum=12
who completed courseIDNum=195
。手上,Esther B
fromdistrictIDNum=5
已经完成courseIDs 11, 177 and 128
;因此,她的数据应该显示所有 3 个 courseID。数据也应该显示Tommy L
已经完成courseIDs 12 and 68
。
以下是示例表:
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