【问题标题】:Access Query Find All records Where all Linked Records Match访问查询查找所有链接记录匹配的所有记录
【发布时间】:2017-10-16 06:01:19
【问题描述】:

表格:

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 查询是什么?

【问题讨论】:

    标签: sql ms-access


    【解决方案1】:

    如果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);
    

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

      • 但是如果一个学生只注册了三个班级(但所有三个班级都取得了不错的成绩),那么您的 SQL 将仅仅因为他没有注册所有五个班级而将他排除在结果之外。我不确定这是不是故意的。我会删除 WHERE 子句的那部分。
      • 是的,如果这无关紧要,那么我们必须删除检查计数的 where 子句
      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 2012-07-31
      • 2018-04-27
      • 2013-02-18
      • 2015-02-02
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多