我有两个表,gradereport
和student
gradereport 表格中有 studentNo ,这是学生表中的外键,并且每个学生都有成绩,< / p>
我必须查询年级报告表格,找出哪些学生没有所有成绩&#39; A&#39;在他们的课程中。
如果他们有任何成绩不是&#39; A&#39;然后他们不应该被包括在结果中。
如何查询表格年级报告,以查找在课程中没有全部学生的学生。
年级报告表
学生表
答案 0 :(得分:2)
没有获得所有A级成绩的学生可以找到
select s.*,
gr.section,
gr.grade
from student s
join gradereport gr on gr.studentNo = s.studentnumber
group by s.studentnumber,gr.section
having sum(gr.grade='A') <> count(*) ;
或者,如果您不关心section
那么
select s.*
from student s
join gradereport gr on gr.studentNo = s.studentnumber
group by s.studentnumber
having sum(gr.grade='A') <> count(*)
答案 1 :(得分:1)
你可以像这样进行独家检查
SELECT distinct studentno FROM gradereport gr
WHERE gr.grade = "A"
AND NOT EXISTS
( SELECT 1
FROM gradereport
WHERE grade <> "A"
AND studentno = gr.studentno
)
您也可以进行独家加入(我的偏好)
SELECT DISTINCT gr.studentno
FROM gradereport gr
LEFT JOIN gradereport gr1 ON gr1.studentno = gr.studentno AND gr1.grade <> "A"
WHERE gr1.studentno is null
答案 2 :(得分:0)
试试这个:
SELECT student.name, student.number
FROM student, gradereport
WHERE student.number = gradereport.StudentNumber
AND gradereport.Grade != 'A'
答案 3 :(得分:0)
查找所有至少有一个A年级的学生:
SELECT DISTINCT `StudentNo` FROM `gradereport` WHERE `Grade` = 'A'
找到对面结果:
SELECT `StudentNo` FROM `gradereport` WHERE `StudentNo` NOT IN (SELECT DISTINCT `StudentNo` FROM `gradereport` WHERE `Grade` = 'A')
答案 4 :(得分:0)
我做这样的事情:
SELECT
DISTINCT StudentNo
FROM
gradereport GR
WHERE
NOT EXISTS (
SELECT 1
FROM gradereport GR2
WHERE
GR.StudentNo = GR2.StudentNo
AND
GR2.Grade != 'A'
)