访问查询查找所有链接记录匹配的所有记录

时间:2017-10-16 06:01:20

标签: sql ms-access

表:

tblStudents:
ID, Student Name
1, John
2, Mark
3, Fred

tblEnrolledSubjects:
ID, Student ID, Subject ID, Score
1, 1, 1, 75
2, 1, 2, 75
3, 1, 3, 75
4, 1, 4, NULL
5, 2, 1, 75
6, 3, 1, 75
7, 3, 2, 80
8, 3, 3, 85

tblSubject:
ID, Subject Name
1, Maths
2, English
3, Science
4, History

我希望为所有科目返回所有分数超过(比如70)的学生。选择[学生姓名]

学生可以注册一门或多门科目。 如果学生甚至有一个不具有所需分数的科目,则不应列出。

从他上面的数据我希望看到

Mark
Fred

SQL查询对此有何影响?

2 个答案:

答案 0 :(得分:0)

如果NULL值对您没有影响,那么

SELECT tblStudents.[Student Name]
FROM tblEnrolledSubjects RIGHT JOIN tblStudents 
    ON tblEnrolledSubjects.[Subject ID] = tblStudents.ID
GROUP BY tblStudents.[Student Name]
HAVING (Min(tblEnrolledSubjects.[Score])>=70);

答案 1 :(得分:0)

SELECT t.Studentname
  FROM tblStudents t JOIN
      (select count(*) cnt, min(nz(score,0)) minscore, StudentID
         FROM tblEnrolledSubjects
     GROUP BY StudentID) s
   ON t.Studentid = s.studentid
WHERE minscore>=70 --if mark is greater than or equal to 70