MySQL:列出教授CS160部分和99F术语CS340部分的教师(姓名)

时间:2010-10-28 04:15:57

标签: mysql select

架构如下:

Student(Snum, Sname)
Course(Cnum, Cname)
Professor(Pnum,Pname, Dept, Office)
Class(Cnum, Term, Section, Instructor)

如何加入以下两个选项以获得教授CS160和CS340的教练?

SELECT DISTINCT Instructor FROM class
WHERE Term = "99F" AND Cnum = "CS160"
SELECT DISTINCT Instructor FROM class
WHERE Term = "99F" AND Cnum = "CS340"

谢谢!

1 个答案:

答案 0 :(得分:1)

由于MySql没有intersect,你必须进行自我加入;类似的东西:

SELECT DISTINCT a.Instructor FROM class a inner join class b
using (Instructor,Term)
where a.Term "99F" and a.Cnum = "CS160" and b.Cnum = "CS340"

编辑:相交,你只需将intersect说明符放在你的例子中的2个查询之间(你可以省略“distinct”;“intersect”只返回distinct值):

SELECT Instructor FROM class
WHERE Term = "99F" AND Cnum = "CS160"
INTERSECT
SELECT Instructor FROM class
WHERE Term = "99F" AND Cnum = "CS340"

intersect是SQL标准的一部分,但MySql没有实现它。具有intersect的SQL实现包括Oracle和Postgres。

另见mySQL versus Standard SQL