我目前正在学习期末考试,上一门考试的其中一个学习问题如下:

您具有以下架构:
学生(Snum:整数,Sname:字符串,Major:字符串,Class:字符串,Age:字符串)
课程(Cname:字符串,Meets_at:时间,Room:字符串,Fid:整数)
系(Fid:整数,Fname:字符串,Deptid:整数)
已注册(Snum:整数,Cname:整数)

我遇到的问题是:


  “找到在每个房间里教书的所有教师的名字
  一些班级相遇”


这是我到目前为止的内容:

SELECT DISTINCT( Fname )
FROM (FACULTY NATURAL JOIN COURSE AS C1), COURSE AS C2
WHERE C1.Room = C2.Room
GROUP BY Fname,C2.Cname
WHERE COUNT(C1.Room) = (SELECT COUNT(DISTINCT(Room))
                        FROM COURSE
                        WHERE Cname = C2.Cname);


我认为自己走在正确的道路上,但是我不确定这是否正确。任何帮助,将不胜感激!

最佳答案

是的,我认为您的策略正确,即将每位教员的人数与课程表上不同房间的人数进行匹配。

请尝试以下查询:

SELECT DISTINCT F.Fname
FROM
Faculty AS F
INNER JOIN
    (SELECT Fid, COUNT(DISTINCT Room) AS cnt FROM Course
    GROUP BY Fid
    ) rooms_per_faculty
    ON F.Fid = rooms_per_faculty.Fid
INNER JOIN
    (SELECT COUNT(DISTINCT Room) AS cnt FROM Course) rooms_that_meet
    ON rooms_per_faculty.cnt = rooms_that_meet.cnt


(可能会帮助您获得一些数据。在下面的示例中,只有Dr.4符合标准-在所有三个教室中进行教学-A,B,C)

create local temporary table course(Room varchar(1), Fid int);
create local temporary table faculty(Fid int, Fname varchar(3));
insert into faculty
    select 1, 'DR1'
union select 2, 'DR2'
union select 3, 'DR3'
union select 4, 'DR4';

insert into course
    select 'A',1
union select 'B',1
union select 'A',2
union select 'C',3
union select 'B',4
union select 'A',4
union select 'C',4;

关于mysql - SQL集划分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20649252/

10-13 02:16