mysql复盘,⑥子查询

– 查询比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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值