0

使用“ AND ”运算符获取查询以检索所需结果时遇到困难。需要显示完成了 3 门课程中的任何一个的人的firstname, lastname,emailcourseIDin districtIDNum=5,即(Health Old (courseIDNum=11) OR Health New (courseIDNum=12)) AND (PE Old (courseIDNum=68 OR PE New (courseIDNum) =177)) AND courseIDNum=128 . 请知道1112实际上是同一门课程:一个是旧课程,另一个是新课程(与68and相同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=12who completed courseIDNum=195。手上,Esther BfromdistrictIDNum=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

4

2 回答 2

1

你陈述查询条件的方式

 s.CourseIDNum IN (11, 12)
 AND s.CourseIDNum IN (68, 177)
 AND s.CourseIDNum=128

意味着 CourseIDNum 必须同时等于一组整数。

SELECT p.FirstName,
       p.LastName,
       p.Email
FROM People p
WHERE p.PeopleID IN
(
    SELECT r.PeopleIdNum
    FROM Registration r
    INNER JOIN Section s
       on r.SectionIDNum = s.SectionID
    INNER JOIN School sc
       on p.SchoolIDNum = sc.SchoolID
    WHERE s.CourseIDNum IN (11, 12, 68, 177, 128)
      AND sc.DistrictIDNum = 5
      AND r.Completed='Y'
)

获得人员列表后,通过连接部分记录来检索他们的 CourseID,如此处或此处所示。

于 2013-05-23T05:40:09.403 回答
0
WHERE s.CourseIDNum IN (11, 12)
  AND s.CourseIDNum IN (68, 177)
  AND s.CourseIDNum=128

您的这部分意味着您正在编写类似这样的查询

WHERE s.CourseIDNum IN (11,12,68,177) and courseIDNum = 128

这意味着 CourseID num 在一种情况下可以等于 11 , 12, 68, 177 ,同时它应该等于 128 .... 这是不可能的。所以我不确定,但尝试使用like

WHERE (s.CourseIDNum IN (11, 12,68,177)
  OR s.CourseIDNum=128)
于 2013-05-23T05:27:16.477 回答