Mysql 哪些其他教师正在教授John Cullen教授的课程。SQL
我有多个表,但对于这个查询,只需要两个表,即Faculty和CourseSection。 我尝试了以下方法,但失败了。如果有人可以帮助Mysql 哪些其他教师正在教授John Cullen教授的课程。SQL,mysql,sql-server,xquery-sql,Mysql,Sql Server,Xquery Sql,我有多个表,但对于这个查询,只需要两个表,即Faculty和CourseSection。 我尝试了以下方法,但失败了。如果有人可以帮助 Select faculty.firstname,faculty.lastname,CourseSection.facid,courseID from CourseSection,faculty where CourseSection.facid in( Select CourseSection.CourseID from CourseSection whe
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')
您应该在表之间使用左联接(假设您的表与CourseSection.facid=faculty.facid相关)
您需要在
FacID
上加入CourseSection
以查找“John Cullen”教授的课程,然后在CourseID
上再次加入CourseSection
以查找所选课程的所有教师(我想不包括“John Allen”),最后再次加入教员
获取教师数据(点击进行演示):
选择f2.facid、f2.firstname、f2.lastname、c2.courseID
从f学院加入f.facid=c.facid上的c部分课程
在c.CourseID=c2.CourseID上加入CourseSection c2
在c2.facid=f2.facid和f.facid f2.facid上连接教员f2
其中f.FirstName='John'和f.LastName='Cullen';
发布您的表模式将有助于确定如何在此构建正确的语句您尝试过我的ans吗?是@knowledge。。。。但它不起作用。你能不能只显示两个表格,并要求输出?那么我可以帮你MySQL或Sql Server吗?@W.Ali“不工作”是什么意思。。你有错误吗?错误的结果?没有结果?你的形象没用。。在比较CourseSection.FacID和CourseSection.CourseID时,显示正确的数据样本和清晰的导出结果会使图像更加清晰。实际上,它不起作用,也不应该起作用。尽管如此,这个答案还是被高估了,我觉得有点可笑,我必须说:-)可能是。。但在答案的开头清楚地显示了用于连接的假设。。。然后OP可以判断这是对还是错但是。。。他对此一无所知…希望你的建议能帮助OP定义实际的连接条件。。
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')
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';