嵌套的sql语句,有点复杂,请大侠解析
CREATE TABLE student
(
sno VARCHAR(10) PRIMARY KEY,
sname VARCHAR(20),
sage DECIMAL(2),
ssex VARCHAR(5)
) ;
CREATE TABLE teacher
(
tno VARCHAR(10) PRIMARY KEY,
tname VARCHAR(20)
) ;
CREATE TABLE course
(
cno VARCHAR(10),
cname VARCHAR(20),
tno VARCHAR(20),
CONSTRAINT pk_course PRIMARY KEY ( cno, tno )
) ;
CREATE TABLE sc
(
sno VARCHAR(10),
cno VARCHAR(10),
score DECIMAL(4, 2),
CONSTRAINT pk_sc PRIMARY KEY ( sno, cno )
) ;
/*******初始化学生表的数据******/
insert into student values ('s001','张三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','吴鹏',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王丽',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','刘玉',21,'男');
insert into student values ('s008','萧蓉',21,'女');
insert into student values ('s009','陈萧晓',23,'女');
insert into student values ('s010','陈美',22,'女');
/******************初始化教师表***********************/
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '谌燕');
insert into teacher values ('t003', '胡明星');
/***************初始化课程表****************************/
insert into course values ('c001','J2SE','t002');
insert into course values ('c002','Java Web','t002');
insert into course values ('c003','SSH','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t002');
insert into course values ('c008','DIV+CSS','t001');
insert into course values ('c009','PHP','t003');
insert into course values ('c010','EJB3.0','t002');
/***************初始化成绩表***********************/
insert into sc values ('s001','c001',78.9);
insert into sc values ('s002','c001',80.9);
insert into sc values ('s003','c001',81.9);
insert into sc values ('s004','c001',60.9);
insert into sc values ('s001','c002',82.9);
insert into sc values ('s002','c002',72.9);
insert into sc values ('s003','c002',81.9);
insert into sc values ('s001','c003',59);
问题:查询没学过“谌燕”老师课的同学的学号、姓名;
[解决办法]
select sno,sname from student where sno not in
(select sno from sc a,course b,teacher c where a.cno=b.cno and b.tno=c.tno and tname='谌燕') a
[解决办法]
这应该是教科书上的答案
SELECT *
FROM student
WHERE sno IN ( SELECT sno
FROM sc
WHERE cno IN ( SELECT cno
FROM course
WHERE tno NOT IN ( SELECT tno
FROM teacher
WHERE tname <> '谌燕' ) ) )