2017-03-25 27 views
2

enter image description here我有多個表,但是對於這個查詢,只需要兩個表即Faculty和CourseSection。 我試過以下但失敗了。如果有人可以幫忙。其他教師正在教授John Cullen正在教授的課程。SQL

Select faculty.firstname,faculty.lastname,CourseSection.facid,courseID 
from CourseSection,faculty 
where CourseSection.facid in(
Select CourseSection.CourseID 
from CourseSection 
where CourseSection.facid =Faculty.facid and firstname='John' AND lastname='Cullen') 
+1

發佈您的表圖式,將有助於確定如何在這裏建立正確的說法 – Rogue

+0

你試試我的答? –

+0

是@knowledge ....但它不能正常工作 –

回答

2

您應該使用左邊的表(assumin你表由CourseSection.facid = faculty.facid相關)

Select 
     faculty.firstname 
     ,faculty.lastname 
     , CourseSection.facid 
     , CourseSection.courseID 
    from CourseSection 
    LEFT JOIN faculty on CourseSection.facid = faculty.facid 
    where CourseSection.facid in (
    Select CourseSection.CourseID 
     from CourseSection 
     where CourseSection.facid =Faculty.facid 
     and firstname='John' AND lastname='Cullen') 
+0

對不起 - 誤讀了 –

+0

它不能正常工作 –

+0

@ W.Ali你是什麼意思與「不工作」..你有錯誤?錯誤的結果..?沒有結果 ? – scaisEdge

0

您需要在FacIDCourseSection加入Faculty一次之間的連接找到'John Cullen'教授的課程,再與CourseSection聯繫CourseID找到所有選定課程的教師(不包括'John Allen',我想),最後再加入Faculty獲得教師資料(點擊here用於演示):

select f2.facid, f2.firstname,f2.lastname, c2.courseID 
from Faculty f join CourseSection c on f.facid = c.facid 
join CourseSection c2 on c.CourseID = c2.CourseID 
join Faculty f2 on c2.facid = f2.facid and f.facid <> f2.facid 
where f.FirstName = 'John' and f.LastName = 'Cullen';