找出沒有選修過“李明”老師講授課程的所有學生姓名


Select S.Sno,S.Sname 
from S 
where Sno not in 
(select distinct( SC.Sno) 
from SC,C,T
where  SC.Cno=C.Cno and T.Tno=C.Tno and T.Tname='李明'); 
Select S.Sno,S.Sname 
from S 
where  not exists 
(select *
from SC,C,T
where  S.Sno=SC.Sno and SC.Cno=C.Cno and T.Tno=C.Tno and T.Tname='李明'); 

以上兩種方法都是正確的。


類似於!= '李明' 的方法不可以。

因為如果一個學生同時選擇了李明和張勇的課,雖然排除了選修的李明的課,但因為該生選修有張勇的課,該學生仍舊會在結果記錄中。
很明顯,這個同學也選了李明的課,不符合題目條件。
同樣道理,類似的解法都不對。
這樣的方法只是排除了只選修了李明老師課的同學。
錯誤1:
Select S.Sno,S.Sname 
from S 
where Sno  in 
(select distinct( SC.Sno) 
from SC,C,T
where  SC.Cno=C.Cno and T.Tno=C.Tno and T.Tname!='李明'); 

錯誤2:

Select distinct S.Sno,S.Sname 
from S,SC,C,T
where  S.Sno=SC.Sno and SC.Cno=C.Cno and T.Tno=C.Tno and T.Tname!='李明'; 

 


附錄1:建表語句
Create table S 
(Sno char(9) PRIMARY KEY,   
 Sname char(20),
 Sage int,   
 Ssex char(2))  
      
Create table T
(Tno char(5) PRIMARY KEY,    
 Tname char(20))               
          
Create table C
(Cno char(4) PRIMARY KEY,   
 Cname char(40),  
 Tno char(5),
 FOREIGN KEY(Tno) REFERENCES T(Tno))              
       
Create table SC
(Sno char(9),  
Cno char(4),  
Score int,
PRIMARY KEY (Sno,Cno),  
FOREIGN KEY(Sno) REFERENCES S(Sno),
FOREIGN KEY(Cno) REFERENCES C(Cno))

 附錄2:測試數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM