2013-10-17 39 views
0

我試圖提供一個包含採用系統分析和項目管理課程的學生的列表。子查詢 - 列出兩個課程

當我單獨選擇'系統分析'或'項目管理'時,它會給我結果,但不是我所需要的。我需要參加兩門課程的學生。

SELECT FIRST_NAME, LAST_NAME, PHONE 
FROM STUDENT d,ENROLLMENT f,SECTION g,COURSE h 
WHERE d.STUDENT_ID = f.STUDENT_ID AND f.SECTION_ID = g.SECTION_ID AND g.COURSE_NO = h.COURSE_NO 
AND h.DESCRIPTION = 'Systems Analysis' AND h.DESCRIPTION = 'Project Management' 
AND DESCRIPTION NOT IN 
(SELECT m.DESCRIPTION 
FROM STUDENT z,ENROLLMENT b,SECTION w,COURSE m 
WHERE z.STUDENT_ID = b.STUDENT_ID AND b.SECTION_ID = w.SECTION_ID AND w.COURSE_NO = m.COURSE_NO 
AND m.DESCRIPTION = 'Systems Analysis' AND m.DESCRIPTION = 'Project Management') 
ORDER BY FIRST_NAME, LAST_NAME, PHONE; 

但是當我選擇這兩個「系統分析」和「項目管理」它給了我沒有行

SELECT FIRST_NAME, LAST_NAME, PHONE 
FROM STUDENT d,ENROLLMENT f,SECTION g,COURSE h 
WHERE d.STUDENT_ID = f.STUDENT_ID AND f.SECTION_ID = g.SECTION_ID AND g.COURSE_NO = h.COURSE_NO 
AND h.DESCRIPTION = 'Systems Analysis' AND h.DESCRIPTION = 'Project Management' 
AND DESCRIPTION NOT IN 
(SELECT m.DESCRIPTION 
FROM STUDENT z,ENROLLMENT b,SECTION w,COURSE m 
WHERE z.STUDENT_ID = b.STUDENT_ID AND b.SECTION_ID = w.SECTION_ID AND w.COURSE_NO = m.COURSE_NO 
AND m.DESCRIPTION = 'Systems Analysis' AND m.DESCRIPTION = 'Project Management') 
ORDER BY FIRST_NAME, LAST_NAME, PHONE; 

如何提供包含誰採取兩個系統的分析與學生名單項目管理課程不給我沒有行?

回答

1

試試這個:

SELECT FIRST_NAME, LAST_NAME, PHONE 
FROM STUDENT d,ENROLLMENT f,SECTION g,COURSE h 
WHERE d.STUDENT_ID = f.STUDENT_ID AND f.SECTION_ID = g.SECTION_ID AND g.COURSE_NO = h.COURSE_NO 
AND h.DESCRIPTION = 'Systems Analysis' 
INTERSECT 
SELECT FIRST_NAME, LAST_NAME, PHONE 
FROM STUDENT d,ENROLLMENT f,SECTION g,COURSE h 
WHERE d.STUDENT_ID = f.STUDENT_ID AND f.SECTION_ID = g.SECTION_ID AND g.COURSE_NO = h.COURSE_NO 
AND h.DESCRIPTION = 'Project Management' 
+0

它確實工作!謝謝...不知道如果我明白答案 – Ris

+0

看看這個:http://www.techonthenet.com/sql/intersect.php – Chandu

+0

謝謝,這是想想 – Ris

1

或者

SELECT stud.FIRST_NAME, stud.LAST_NAME, stud.PHONE 
FROM STUDENT stud, 
inner join ENROLLMENT enr On stud.Student_ID = enr.student_ID 
inner join SECTION sec On sec.Section_ID = enr.Section_ID 
inner join COURSE syst On syst.course_no = sec.Course_No 
inner join COURSE proj On proj.Course_no = sec.Course_No 
Where syst.DESCRIPTION = 'Systems Analysis' 
AND proj.DESCRIPTION = 'Project Management' 
ORDER BY stud.FIRST_NAME, stud.LAST_NAME, stud.PHONE 

如果我通過這些別名正確翻遍了,你可以讓你知道有意義的。哦,並使用正確的連接語法。招數是加入兩次,一次用於系統分析,一次用於項目管理。

PS如果在那裏有一個SQL課程,我建議你接受它。

+0

謝謝!我喜歡你的風格。 – Ris