MySQL:列出在 99F 学期教授 CS160 部分和 CS340 部分的教师(姓名)

发布于 2024-09-29 02:23:04 字数 391 浏览 6 评论 0原文

架构如下:

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"

谢谢!

The schema is as follows:

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

How can I join the two selects below to get Instructors who taught both CS160 and CS340?

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2024-10-06 02:23:04

由于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 说明符放在示例中的两个查询之间(并且可以省略“distinct”;“intersect”仅返回不同的值):

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

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

另请参阅 mySQL 与标准 SQL

Since MySql doesn't have intersect, you have to do a self-join; something like:

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"

Edit: with intersect, you just put the intersect specifier between the 2 queries you had in your example (and you can omit the "distinct"; "intersect" returns only distinct values):

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

intersect is part of the SQL standard, but MySql doesn't implement it. SQL implementations that do have intersect include Oracle and Postgres.

See also mySQL versus Standard SQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文