我如何为 ALL 条件编写此 SQL 查询?
How do i write this SQL query for an ALL condition?
我需要在 MySQL 中构建一个 SQL 查询来解决 class 作业中的一个问题。我已经在这个问题上工作了一段时间,但我很难弄清楚如何正确地构造这个查询。总的来说,我对 SQL 语言和数据库还比较陌生,我对这个问题感到困惑。到目前为止,我已经发布了我的想法,但不幸的是我无法获得我正在寻找的结果。如果有人能给我一些关于如何实现这一点的指导,我将不胜感激。
这是我的 table 结构:
course(cid, fid, room)
enroll(cid, sid, grade)
faculty(fid, fname, dept, rank)
student(sid, sname, major, credits)
这是我需要构建的查询:
Show the faculty id and faculty name for all faculty that have taught all computer science majors (major = 'CSC').
这是我目前尝试过的方法:
select f.fid, f.fname
from faculty f
join course c
on f.fid = c.fid
join enroll e
on c.cid = e.cid
join student s
on e.sid = s.sid
where s.sid = ALL
(select sid
from student
where major = 'CSC');
select f.fid, f.fname
from faculty f
join course c
on f.fid = c.fid
join enroll e
on c.cid = e.cid
join student s
on e.sid = s.sid
group by f.fid, s.sid
having s.sid = ALL
(select sid
from student
where major = 'CSC'));
我很难理解的逻辑障碍是如何确保教员正在教授所有当前 CSC 专业。您可以看到我尝试添加一些逻辑来检查每条记录 returned,但恐怕我可能误解了语法。这些查询将 运行,但它们 return 空集。感谢您的帮助。
你做的看起来不错。我想你可能想多了。从逻辑上讲,这应该会为您提供所需的内容:
Select f.fid, f.fname
from faculty f
join course c on c.fid = f.fid
join enroll e on e.cid = c.cid
join student s on s.sid = e.sid
Where major = 'CSC'
group by f.fid, f.fname
试试
select f.fid, f.fname
from faculty f
join course c
on f.fid = c.fid
join enroll e
on c.cid = e.cid
join student s
on e.sid = s.sid
where s.sid IN (select sid from student where major = 'CSC');
我同意这个问题可能不清楚,而且他们可能只是教过 任何 CSC 专业的所有教师。但是,以防万一您仍然需要所有教过 all CSC 专业的教师,这应该可行:
以下查询告诉我们教师和 CSC 专业的对:
select f.fid, s.sid
from faculty f
inner join course c
on f.fid = c.fid
inner join enroll e
on e.cid = c.cid
inner join student s
on e.sid = s.sid
where s.major = 'CSC'
group by f.fid, s.sid
因此,如果我们知道计算机科学专业的学生人数:
select count(1)
from student s
where s.major = 'CSC'
然后我们可以将每位教员所教授的CSC专业数相加,看是否等于CSC专业总数:
select b.fid, b.fname
from (
select a.fid, a.fname, count(1) as taught_count
from (
select f.fid, f.fname, s.sid
from faculty f
inner join course c
on f.fid = c.fid
inner join enroll e
on e.cid = c.cid
inner join student s
on e.sid = s.sid
where s.major = 'CSC'
group by f.fid, s.sid
) a
group by a.fid, a.fname
) b
where b.taught_count = (
select count(1)
from student s
where s.major = 'CSC'
)
我需要在 MySQL 中构建一个 SQL 查询来解决 class 作业中的一个问题。我已经在这个问题上工作了一段时间,但我很难弄清楚如何正确地构造这个查询。总的来说,我对 SQL 语言和数据库还比较陌生,我对这个问题感到困惑。到目前为止,我已经发布了我的想法,但不幸的是我无法获得我正在寻找的结果。如果有人能给我一些关于如何实现这一点的指导,我将不胜感激。
这是我的 table 结构:
course(cid, fid, room)
enroll(cid, sid, grade)
faculty(fid, fname, dept, rank)
student(sid, sname, major, credits)
这是我需要构建的查询:
Show the faculty id and faculty name for all faculty that have taught all computer science majors (major = 'CSC').
这是我目前尝试过的方法:
select f.fid, f.fname
from faculty f
join course c
on f.fid = c.fid
join enroll e
on c.cid = e.cid
join student s
on e.sid = s.sid
where s.sid = ALL
(select sid
from student
where major = 'CSC');
select f.fid, f.fname
from faculty f
join course c
on f.fid = c.fid
join enroll e
on c.cid = e.cid
join student s
on e.sid = s.sid
group by f.fid, s.sid
having s.sid = ALL
(select sid
from student
where major = 'CSC'));
我很难理解的逻辑障碍是如何确保教员正在教授所有当前 CSC 专业。您可以看到我尝试添加一些逻辑来检查每条记录 returned,但恐怕我可能误解了语法。这些查询将 运行,但它们 return 空集。感谢您的帮助。
你做的看起来不错。我想你可能想多了。从逻辑上讲,这应该会为您提供所需的内容:
Select f.fid, f.fname
from faculty f
join course c on c.fid = f.fid
join enroll e on e.cid = c.cid
join student s on s.sid = e.sid
Where major = 'CSC'
group by f.fid, f.fname
试试
select f.fid, f.fname
from faculty f
join course c
on f.fid = c.fid
join enroll e
on c.cid = e.cid
join student s
on e.sid = s.sid
where s.sid IN (select sid from student where major = 'CSC');
我同意这个问题可能不清楚,而且他们可能只是教过 任何 CSC 专业的所有教师。但是,以防万一您仍然需要所有教过 all CSC 专业的教师,这应该可行:
以下查询告诉我们教师和 CSC 专业的对:
select f.fid, s.sid
from faculty f
inner join course c
on f.fid = c.fid
inner join enroll e
on e.cid = c.cid
inner join student s
on e.sid = s.sid
where s.major = 'CSC'
group by f.fid, s.sid
因此,如果我们知道计算机科学专业的学生人数:
select count(1)
from student s
where s.major = 'CSC'
然后我们可以将每位教员所教授的CSC专业数相加,看是否等于CSC专业总数:
select b.fid, b.fname
from (
select a.fid, a.fname, count(1) as taught_count
from (
select f.fid, f.fname, s.sid
from faculty f
inner join course c
on f.fid = c.fid
inner join enroll e
on e.cid = c.cid
inner join student s
on e.sid = s.sid
where s.major = 'CSC'
group by f.fid, s.sid
) a
group by a.fid, a.fname
) b
where b.taught_count = (
select count(1)
from student s
where s.major = 'CSC'
)