关键词:“ 学生姓名、张三老师“
第一步: 锁定表,需要用到全部的四张表,张三老师在teacher
表,老师教授的课程在course
表,但course
表与学生表没有直接关系,所以需要通过中间成绩表sc
,然后关联到学生表student
获取学生信息
第二步: 关联课程表和教师表,筛选出张三老师教授的课程Cid
SELECT * FROM course JOIN teacher ON course.`TId` = teacher.`TId` WHERE teacher.`Tname` = '张三';
第三步: 关联成绩表,查询出哪些学生学了张三老师的任意一门课程
SELECT DISTINCT sc.`SId` FROM sc
JOIN course c ON sc.`CId` = c.`CId`
JOIN teacher t ON c.`TId` = t.`TId`
WHERE t.`Tname` = '张三';
第四步:使用not in
获取没有学过张三老师任意一门课程的学生姓名
SELECT student.`Sname` FROM student
WHERE sid NOT IN
(SELECT DISTINCT sc.`SId` FROM sc
JOIN course c ON sc.`CId` = c.`CId`
JOIN teacher t ON c.`TId` = t.`TId`
WHERE t.`Tname` = '张三');