如何为复杂的select语句添加条件

时间:2014-09-04 16:22:15

标签: mysql sql

我有这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-显示有学生完成要求的课程

SELECT c.*, sc.* FROM semestercourses AS sc 
NATURAL LEFT JOIN course AS c 
NATURAL LEFT JOIN courserequirements AS cr
NATURAL LEFT JOIN studentcoursecomplete   AS scc
WHERE 
sc.CourseID NOT IN (SELECT CourseID FROM studentcoursecomplete)
AND 
(cr.RequirementID IS NULL OR cr.RequirementID IN (SELECT CourseID FROM
studentcoursecomplete)) 

声明使其正确..
但我怎么能为一个有针对性的学生做这件事 例如studentcoursecomplete.StudentID = 1
!我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

SELECT
courseid
,coursenum
,coursename
FROM course 
WHERE
courseid not in (SELECT courseid from studentcoursecomplete where studentid =1)
and courseid in (SELECT courseid from courseRequirements where ReqID is not NULL)
and courseid in (SELECT courseid from semester courses)

希望这有效,我理解你在寻找什么。