简体   繁体   English

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

[英]Access Query Find All records Where all Linked Records Match

Tables: 表格:

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

I want to return all distinct students that have a score of over (say 70) for all subjects. 我想返回所有学科得分都超过(例如70分)的不同学生。 SELECT [Student Name] 选择[学生姓名]

Students can be enrolled in 1 or more subjects. 学生可以注册1个或多个科目。 If a student has even one subject not of required score then they should not be listed. 如果学生甚至有一门学科没有达到要求的分数,则不应列出他们。

From he above data I would expect to see 从他上面的数据我希望看到

Mark
Fred

What would the SQL query be for this? SQL查询将对此做什么?

If a NULL value doesn't count against you, then 如果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);
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM