![](/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.