2013-05-10 52 views
0

我有一个数据库,它是这样的:难Oracle查询

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天。你可以帮帮我吗?我正在使用Oracle。

+1

'我一直在尝试2天 - 你可以显示一些你已经尝试过的查询,哪些不起作用? – 2013-05-10 05:10:22

+0

有问题 - 我找不到所需查询的逻辑:| – CuriousGuy 2013-05-10 05:22:06

回答

0

你需要的是先计算所有可能的对学生,教师,然后减去已满足学生教师:

首先是学生和教师的双重交叉进行。与第二基于上过的课程加入:

SELECT studentid, teacherid from students, teachers 

EXCEPT 

select studentid, teacherid from stu_cou natural join tea_cou; 

如果你有兴趣的学生的名字和老师的名字,你可以用这个结果作为一个子查询并执行连接到学生和教师的表来获取信息。但我会把它作为一个练习让你去做。

--dmg

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 
) 
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 
) 

基本上,学生和老师的每一个可能的组合,有没有已经课程由那个学生出席并由那位老师教导?