我有这4张桌子
1. course
------------------------
CourseID CourseNum CourseName
2. courseRequirements
------------------------
CourseID ReqID
3. semestercourses
------------------------
CourseID
4. studentcoursecomplete
-----------------------
CourseID StudentID
声明必须执行下面的
1 - 显示课程名称,其中包括课程课程。课程ID = semestercourses.CourseID
2 - 不要显示学生完成“studentcoursecomplete”的课程
3 - 不要显示要求学生没有完成的课程
4-显示有学生完成要求的课程
示例
本学期有3门课程(英语2,数学,运动)我有这个陈述
SELECT course.*
FROM course
LEFT JOIN studentcoursecomplete ON studentcoursecomplete.CourseID = course.CourseID
LEFT JOIN courseRequirements ON courseRequirements.CourseID = course.CourseID
WHERE courseRequirements.CourseID IS NULL
AND studentcoursecomplete.CourseID IS NULL
这句话工作正常,但如果学生完成英语1,则英语2没有显示
答案 0 :(得分:0)
我没有看到您正在纳入目前为特定学期宣布的课程。以数据库结构为基础,以下查询将生成您所描述的所需效果。
SELECT c.* FROM semestercourses AS sc
LEFT JOIN course AS c ON sc.CourseID = c.CourseID
LEFT JOIN courserequirements AS cr ON cr.ReqID = c.CourseID
LEFT JOIN studentcoursecomplete AS scc ON scc.CourseID = c.CourseID
WHERE
scc.CourseID IS NULL
虽然未达到先决条件但课程已公布,但不会显示。如果它已经公布并且先决条件得到满足,并且上层课程尚未完成,则可见。在此示例中完成“英语1”,如果宣布英语2(在semestercourses
中),那么它将为您显示。
**编辑:
SELECT c.* FROM semestercourses AS sc
LEFT JOIN course AS c ON c.CourseID = sc.CourseID
LEFT JOIN courserequirements AS cr ON cr.CourseID = sc.CourseID
LEFT JOIN studentcoursecomplete AS scc ON scc.CourseID = sc.CourseID
WHERE
sc.CourseID NOT IN (SELECT CourseID FROM studentcoursecomplete)
AND
(cr.ReqID IS NULL OR cr.ReqID IN (SELECT CourseID FROM studentcoursecomplete))
好的,在对数据进行了一些测试之后做了一些更改。