制定SQL查询

时间:2016-04-21 11:41:12

标签: sql

我刚接触SQL,我必须创建SQL语句以从以下表中进行选择:

studentTable (studentID, firstName, lastName)

enrollmentTable (studentID, courseID, semester, year, grade) 

(studentID和courseID是外键)

courseTable (courseID, semester, year, name)

prereqTable (courseID, prereqID)

我必须选择已注册课程但尚未完成所有先决条件的学生。

编辑:

过了一会儿,我得到了正确的解决方案。关键是要使用多个选择,它就像一个魅力。

2 个答案:

答案 0 :(得分:0)

SELECT * FROM studentTable
         WHERE EXISTS(
           SELECT * FROM enrollmentTable, prereqTable 
                    WHERE enrollmentTable.studentID = studentTable.studentID  
                      AND prereqTable.courseID = enrollmentTable.courseID 
                      AND NOT EXISTS (SELECT * FROM enrollmentTable 
                                              WHERE enrollmentTable.courseID = prereqTable.prereqID
                                                AND enrollmentTable.studentID = studentTable.studentId)
         )

答案 1 :(得分:0)

过了一会儿,我得到了正确的解决方案。 关键是要使用多个选择,它就像一个魅力。