2013-05-10 49 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 
) 

基本上,學生和老師的每一個可能的組合,有沒有已經課程由那個學生出席並由那位老師教導?