2017-03-25 24 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';