![](/img/trans.png)
[英]Microsoft Access sql query to find records where all values in a column match
[英]Access Query Find All records Where all Linked Records Match
表格:
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分)的不同学生。 选择[学生姓名]
学生可以注册1个或多个科目。 如果学生甚至有一门学科没有达到要求的分数,则不应列出他们。
从他上面的数据我希望看到
Mark
Fred
SQL查询将对此做什么?
如果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.