MySQL:给出模式的复杂问题

发布于 2024-09-29 06:35:41 字数 1113 浏览 3 评论 0原文

这是架构:

Student(Snum, Sname)
Course(Cnum, Cname)
Prerequisite(Cnum, Prereq)
Professor(Pnum,Pname, Dept, Office)
Enrollment(Snum, Cnum, Term, Section, Mark)
Schedule(Cnum, Term, Section, Days, Time, Room)
Class(Cnum, Term, Section, Instructor)

完整的问题是:

查找教授(Pnum、Pname、Dept) 谁每当在 阶级关系,无论课程如何 没有任何先决条件或 教授还教授了每一位 课程的先决条件。一个 教授教授一门课程的时间 教授教授了一堂课(a 课程的一部分)在某个术语中。

到目前为止,我的起点...

SELECT * FROM Class c
LEFT JOIN Prerequisite p ON p.Cnum = c.Cnum
WHERE p.Cnum IS NULL

我知道如何找到教授教授的没有先决条件的课程,因此它们满足部分条件。下一步是查明他们是否教授了先决条件(如果有的话)。此外,我当前的查询只为每个类选择一个先决条件,而一个类实际上可能有多个先决条件。

注意:Cnum 和 Prereq 都是 Cnum 的外键

感谢您的帮助!这是一个硬汉。

编辑: 我想出了最终答案!

SELECT prof.Pnum, prof.Pname, prof.Dept FROM Professor prof
WHERE prof.Pnum NOT IN
(
SELECT DISTINCT c.Instructor FROM Class c
RIGHT JOIN Prerequisite p ON p.Cnum = c.Cnum
LEFT JOIN Class cpre ON cpre.Cnum = p.Prereq AND cpre.Instructor = c.Instructor
WHERE cpre.Instructor IS NULL AND c.Instructor IS NOT NULL
)

Here is the schema:

Student(Snum, Sname)
Course(Cnum, Cname)
Prerequisite(Cnum, Prereq)
Professor(Pnum,Pname, Dept, Office)
Enrollment(Snum, Cnum, Term, Section, Mark)
Schedule(Cnum, Term, Section, Days, Time, Room)
Class(Cnum, Term, Section, Instructor)

The full question is:

Find professors (Pnum, Pname, Dept)
who whenever taught a course in the
Class relation, either the course does
not have any prerequisite or the
professor has also taught each of the
prerequisites of the course. A
professor taught a course exactly when
the professor taught a class (a
section of a course) in some term.

My starting point so far...

SELECT * FROM Class c
LEFT JOIN Prerequisite p ON p.Cnum = c.Cnum
WHERE p.Cnum IS NULL

I know how to find the courses that the professors taught that have no prerequisites, and therefore they satisfy part of the condition. The next step is finding if they have taught the prerequisites if there are any. Also, my current query only selects a single prerequisite for every class, when there can really be multiple prerequisites for a class.

Note: Cnum and Prereq are both foreign keys to Cnum

Thanks for any help! This is a toughie.

EDIT:
I came up with the final answer!

SELECT prof.Pnum, prof.Pname, prof.Dept FROM Professor prof
WHERE prof.Pnum NOT IN
(
SELECT DISTINCT c.Instructor FROM Class c
RIGHT JOIN Prerequisite p ON p.Cnum = c.Cnum
LEFT JOIN Class cpre ON cpre.Cnum = p.Prereq AND cpre.Instructor = c.Instructor
WHERE cpre.Instructor IS NULL AND c.Instructor IS NOT NULL
)

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

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

发布评论

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

评论(1

や莫失莫忘 2024-10-06 06:35:41

由于这是作业,这里有一个提示...

如果教授教授了所有先决条件,那么 Pnum 不存在于课程先决条件的子选择中,课程左连接到教授在给定学期教授的实际课程,其中实际class 为 NULL(换句话说,没有实际想到的所教授的课程的先决条件)。

Since this is homework, here's a hint...

If Professor taught all prerequisites then Pnum does not EXISTS in subselect of Prerequisites for a Class' Course left joined to actual Classes that Professor taught in given Term where actual class is NULL (in other words where there are no prerequisites for a class taught that was not actually thought).

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