架构如下:
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"
谢谢!
答案 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。