标签:
student/course
SCORE/TEACHER
例1:查询所有选修了3-245号课程的学生姓名
有以下四种写法:
1.exists写法
首先取Student表中的一个元组,然后在SCORE表中依次找SCORE.Sno=该元组的Sno,并且对应的Cno=‘3-245‘,如果存在,则外层查询的where子句返回为真,则Student表中的该元组可以输出。然后依次遍历Student表中的其他元组。
select SNAME FROM STUDENT where EXISTS(select * from SCORE where STUDENT.SNO = SCORE.SNO and SCORE.CNO = ‘3-245‘)
2. join on写法
select SNAME from STUDENT JOIN SCORE on STUDENT.SNO = SCORE.SNO and SCORE.cno = ‘3-245‘
3.in写法
SELECT SNAME from STUDENT WHERE sno in (select sno from SCORE WHERE SNO = STUDENT.SNO and CNO = ‘3-245‘)
4.引用两个表
SELECT SNAME from STUDENT,SCORE WHERE STUDENT.SNO = SCORE.SNO and SCORE.CNO = ‘3-245‘
结果都是:
例2:查询没有选修3-245号课程的学生姓名
select SNAME FROM STUDENT where NOT EXISTS(select * from SCORE where STUDENT.SNO = SCORE.SNO and SCORE.CNO = ‘3-245‘)
标签:
原文地址:http://www.cnblogs.com/hjhsysu/p/5731677.html