0

我有一个如下所示的数据库:

table_Students: { studentid(PK), name };

table_Stu_cou:{ studentid(FK), courseid(FK) };

table_Courses:{ courseid(PK), coursename };

table_Tea_cou { courseid(FK), teacherid(FK) };

table_Teachers:{ teacherid(PK), name};

stu_cou 表显示哪些学生参加了哪些课程。tea_cou 表显示哪些教师教授哪些课程。我必须列出所有从未见过面的学生和老师(学生从未参加过该讲师讲授的课程)。但我不知道如何制作它,我已经尝试了 2 天。你可以帮帮我吗?我正在使用甲骨文。

4

3 回答 3

1
SELECT s.name, t.name FROM students s CROSS JOIN teachers t
WHERE NOT EXISTS (
    SELECT 1 FROM courses c
    JOIN stu_cou sc ON sc.courseid = c.courseid AND sc.studentid = s.studentid
    JOIN tea_cou tc ON tc.courseic = c.courseic AND tc.teacherid = t.id
)

基本上,对于学生和老师的每种可能组合,是否有该学生参加并由该老师教授的课程?

于 2013-05-10T05:14:19.040 回答
0

您需要首先计算所有可能的配对学生,教师,然后减去已遇到的学生教师:

第一个是学生和教师的交叉产品。第二个是根据所学课程加入的:

SELECT studentid, teacherid from students, teachers

EXCEPT

select studentid, teacherid from stu_cou natural join tea_cou;

如果您对学生姓名和教师姓名感兴趣,您可以将此结果用作子查询并连接到学生和教师表以获取该信息。但我会把它留给你做练习。

--dmg

于 2013-05-10T05:13:28.147 回答
0

这应该可以解决问题,一旦你修正了我的错别字

select t.name, s.name
from  table_Teachers t, table_Students s
where not exists (
    select 'x' 
    from table_Stu_cou sc, table_Tea_cou tc
    where sc.courseid = tc.courseid
    and sc.studentid = s.studentid
    and tc.teacherid = t.teacherid
) 
于 2013-05-10T05:13:39.510 回答