2014-09-04 91 views
0

我有這3個表sql語句複雜條件「加盟」

1. course 
------------------------ 
CourseID  CourseName    
    1   English 1 
    2   English 2 
    3   Math 1 
    4   Math 2 
2. courseRequirements 
------------------------ 
CourseID ReqID     
    2  1 
    4  3 
3. studentcoursecomplete 
----------------------- 
CourseID StudentID  
    1   5 

我需要一個select語句不從表過程如下

  1. 表演課程名稱
  2. 不顯示學生完成「學生課程完成」的過程
  3. 不顯示有學生沒有完成的要求的過程
  4. 顯示,有需求的課程,學生完成

這裏在這個例子中,結果將是

CourseID  CourseName    
    2   English 2 
    3   Math 1 

因爲學生完成英語1,所以他不能看到它
和他沒」 T結束數學1 ..這樣他就看不到數學2

我的select語句做(1,2)
我該怎麼辦(3,4)
3 - 別秀當然,這有一個學生沒完成
4-顯示,有需求的課程,學生完成,你可以做

SELECT course.* 
FROM course 
LEFT JOIN studentcoursecomplete ON studentcoursecomplete.CourseID = course.CourseID 
LEFT JOIN courseRequirements ON courseRequirements.CourseID = course.CourseID 
WHERE studentcoursecomplete.CourseID IS NULL 
+3

*嗅出嗅出*哦,是啊,我愛的功課在上午的味道! – Scozzard 2014-09-04 13:32:20

回答

0

一種方式是通過獲取的那些用戶是不是在和那些要求用戶不能採取和過濾那些出總結果集的..這樣的事情

SELECT c.* 
FROM course c 
LEFT JOIN studentcoursecomplete scc ON scc.CourseID = c.CourseID 
WHERE scc.studentid = 1 
    AND scc.CourseID IS NULL 
    AND c.courseid NOT IN 
( SELECT cr.courseid 
    FROM courseRequirements cr 
    WHERE cr.reqid IN 
    ( SELECT c.courseid 
     FROM course c 
     LEFT JOIN studentcoursecomplete scc ON scc.CourseID = c.CourseID 
     LEFT JOIN courseRequirements cr ON cr.CourseID = c.CourseID 
     WHERE scc.CourseID IS NULL 
      AND EXISTS 
      ( SELECT 1 FROM courserequirements cr1 
       WHERE cr1.CourseID <> c.CourseID 
       AND cr1.reqid = c.courseid 
     ) 
    ) 
) 

DEMO

調整OP的答案,包括特定的用戶ID ...

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 WHERE sid = 1 
) 
    AND (cr.RequirementID IS NULL OR cr.RequirementID IN 
    ( SELECT CourseID 
     FROM studentcoursecomplete WHERE sid = 1 
    ) 
) 
+0

謝謝你的答案@JohnRuddell給我解決方案..這工作正常---------------------------------- ----- 選擇C *,SC。* FROM semestercourses AS SC 自然科學LEFT JOIN當然爲C 自然科學LEFT JOIN courserequirements AS CR LEFT JOIN自然科學AS studentcoursecomplete SCC WHERE sc.CourseID NOT IN(SELECT CourseID FROM studentcoursecomplete) AND (cr.RequirementID IS NULL OR cr.RequirementID IN(SELECT CourseID FROM studentcoursecomplete)) ---------- 但是這個解決方案是針對所有用戶的,我怎樣才能做到這一點StudentID = 1? – 2014-09-04 14:31:17

+0

@GabrielBlanco我剛編輯過,爲學生添加一個過濾器:)嘗試一下,讓我知道這是否適合你! :) – 2014-09-04 14:34:18

+0

@JohnRuddell ..沒有這不工作..查詢給我所有課程..我的查詢工作正常,但我不知道在哪裏把StudentID = 1 – 2014-09-04 14:45:59

0
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)) 
+0

嗯,你可以嘗試把studentid = 1之前,你做任何事情WHERE ...就像'WHERE sc.StudentID = 1 AND sc.CourseID NOT IN'(' – 2014-09-04 14:48:52

+0

我只是做了sc.StudentID,我不知道你是否在學期表中有這個..但是如果你不'然後使用scc.StudentID – 2014-09-04 14:49:30

+0

是的,我有一個semestercourses表,並在那裏CourseID coloumn ..所以我可以做什麼? – 2014-09-04 14:56:12