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:測試數據