– 查询比Tom小的所有学生的信息。
– 1.查询tom的生日
select birthday from student where name=‘tom’;
– 2.查询比Tom小的所有学生的信息
select * from student where birthday>‘1995-07-14’;
– 子查询:把内层查(子查询)询结果供外层(父查询)使用
select * from student where birthday>
(select birthday from student where name=‘tom’);
– 查询年龄最大的学生的信息。-- 单行子查询:子查询的结果返回一个数据
– 2.在写外层查询
select * from student where birthday=
– 1.先写内层查询
(select min(birthday) from student)
– 多行子查询:子查询结果返回多个数据
– 查询比"16级计科一班"所有"Java"成绩都高的"16级计科二班"的学生信息
– 外:比"16级计科一班"所有"Java"成绩都高的"16级计科二班"的学生信息
select student.*,socre from student INNER JOIN classinfo
ON student.cid=classinfo.cid
INNER JOIN score
on score.stuid=student.stuid
INNER JOIN subject
ON subject
.subid=sc ore.subid
where cname=‘16级计科二班’ AND subname=‘java’ and
socre >all(
– 内:16级计科一班"所有"Java"成绩
select SOcre from student INNER JOIN classinfo
ON student.cid=classinfo.cid
INNER JOIN score
on score.stuid=student.stuid
INNER JOIN subject
ON subject
.subid=score.subid
where cname=‘16级计科一班’ AND subname=‘java’);
– 查询比"16级计科一班"任意人"Java"成绩低的"16级计科二班"的学生信息
select student.*,socre from student INNER JOIN classinfo
ON student.cid=classinfo.cid
INNER JOIN score
on score.stuid=student.stuid
INNER JOIN subject
ON subject
.subid=score.subid
where cname=‘16级计科二班’ AND subname=‘java’ and
socre <any(
– 内:16级计科一班"所有"Java"成绩
select SOcre from student INNER JOIN classinfo
ON student.cid=classinfo.cid
INNER JOIN score
on score.stuid=student.stuid
INNER JOIN subject
ON subject
.subid=score.subid
where cname=‘16级计科一班’ AND subname=‘java’);
– in :代表一个集合,子查询(内层)返回多个结果时用in
– 以后=全部用in代替
– 查询参加了考试的所有学生的信息
– 外层:学生信息
select * from student where stuid in
– 内层:哪些学生参加了考试
(select DISTINCT stuid from score);
– 查询未参加考试的所有学生的信息
– 外层:学生信息 – 控制台有没有报错 断点调试 垃圾数据
select * from student where stuid not in
– 内层:哪些学生参加考试
(select DISTINCT stuid from score);
– In:先执行内层在执行外层
– exists:现执行外层,拿着外层的查询结果去内层比较,如果成立返回true
– exists效率比in高
– 查询参加了考试的所有学生的信息
select * from student where EXISTS
(select * FROM score where student.stuid=score.stuid);