sql获得通过所有科目的学生姓名

时间:2015-02-16 14:34:35

标签: sql oracle oracle11g

我有3张桌子

生(ID,姓名)

受试者(ID,姓名)

student_subjects(student_id数据,subject_id,标记)

我想选择通过所有科目的学生姓名(所有科目的分数> = 50)

我试过了:

SELECT DISTINCT s.NAME
FROM STUDENT_SUBJECTS sb,
  STUDENTS s,
  SUBJECTS b
WHERE s.ID   = sb.STUDENT_ID
AND b.ID     = sb.SUBJECT_ID
AND sb.MARK >= 50

但这可以让任何科目大于或等于50的学生 有没有办法得到通过所有科目的学生的名字? oracle schema

1 个答案:

答案 0 :(得分:3)

这应该有效:

SELECT s.NAME
FROM STUDENT_SUBJECTS sb,
  STUDENTS s,
  SUBJECTS b
WHERE s.ID   = sb.STUDENT_ID
AND b.ID     = sb.SUBJECT_ID
GROUP BY S.NAME
HAVING MIN(SB.MARK)>=50

如果存在两名同名的学生,则只按ID分组:

SELECT s.NAME
FROM STUDENT_SUBJECTS sb,
  STUDENTS s,
  SUBJECTS b
WHERE s.ID   = sb.STUDENT_ID
AND b.ID     = sb.SUBJECT_ID
GROUP BY S.ID,S.NAME
HAVING MIN(SB.MARK)>=50