查询以查找在模块中参加考试但未参加必要模块的任何学生的 ID
Query to find the ID of any student that has taken an exam in a module without taking the prerquisite modules
正如标题所说,我正在尝试输出所有已参加模块考试但未参加必备模块考试的学生的 ID。
我使用的表称为学生表、考试表和先决条件表,其布局如下:
STUDENT
Student_name | Student_id | Course_name | Year
Exam
Student_id | Module_code | Exam_year | Score
PREREQUISITES
Module_code | Prerequisite_code
还有其他表格,例如用于先决条件中的外键 module_code 和 prerequisite_code 的模块,但我认为它与此问题无关。
我有疑问来获取问题的每个单独部分,即 Student_id、Module_code 和先决条件代码
SELECT student_id FROM student;
SELECT Module_code FROM exam WHERE Student_id = <EachPreviousFoundID>;
SELECT Prerequisite_code FROM prerequisites WHERE Module_code = <EachPreviousFoundModule>;
但是我不确定如何将这三个查询绑定在一起以创建一个查询,该查询将查找每个特定学生参加的每门考试的模块代码,然后确保他们也参加了先决条件模块。
提前致谢
这是使用集合的方法:
Oracle 设置:
CREATE TYPE stringlist IS TABLE OF VARCHAR2(100);
/
查询:
SELECT e.student_id,
t.COLUMN_VALUE AS module_code,
p.prerequisites MULTISET EXCEPT e.modules_taken AS missing_prerequisites
FROM ( -- All the modules each student has taken.
SELECT student_id,
CAST( COLLECT( module_code ) AS stringlist ) AS modules_taken
FROM exam
GROUP BY student_id
) e,
TABLE( e.modules_taken ) t,
( -- All the prerequisites for each module
SELECT module_code,
CAST( COLLECT( prerequisite_code ) AS stringlist ) AS prerequisites
FROM prerequisites
GROUP BY module_code
) p
WHERE t.COLUMN_VALUE = p.module_code
AND p.prerequisites MULTISET EXCEPT e.modules_taken IS NOT EMPTY;
正如标题所说,我正在尝试输出所有已参加模块考试但未参加必备模块考试的学生的 ID。
我使用的表称为学生表、考试表和先决条件表,其布局如下:
STUDENT
Student_name | Student_id | Course_name | Year
Exam
Student_id | Module_code | Exam_year | Score
PREREQUISITES
Module_code | Prerequisite_code
还有其他表格,例如用于先决条件中的外键 module_code 和 prerequisite_code 的模块,但我认为它与此问题无关。
我有疑问来获取问题的每个单独部分,即 Student_id、Module_code 和先决条件代码
SELECT student_id FROM student;
SELECT Module_code FROM exam WHERE Student_id = <EachPreviousFoundID>;
SELECT Prerequisite_code FROM prerequisites WHERE Module_code = <EachPreviousFoundModule>;
但是我不确定如何将这三个查询绑定在一起以创建一个查询,该查询将查找每个特定学生参加的每门考试的模块代码,然后确保他们也参加了先决条件模块。
提前致谢
这是使用集合的方法:
Oracle 设置:
CREATE TYPE stringlist IS TABLE OF VARCHAR2(100);
/
查询:
SELECT e.student_id,
t.COLUMN_VALUE AS module_code,
p.prerequisites MULTISET EXCEPT e.modules_taken AS missing_prerequisites
FROM ( -- All the modules each student has taken.
SELECT student_id,
CAST( COLLECT( module_code ) AS stringlist ) AS modules_taken
FROM exam
GROUP BY student_id
) e,
TABLE( e.modules_taken ) t,
( -- All the prerequisites for each module
SELECT module_code,
CAST( COLLECT( prerequisite_code ) AS stringlist ) AS prerequisites
FROM prerequisites
GROUP BY module_code
) p
WHERE t.COLUMN_VALUE = p.module_code
AND p.prerequisites MULTISET EXCEPT e.modules_taken IS NOT EMPTY;