MySQL查询篇——(三)子查询

1.=

#查询和‘陈樱’同学同龄的学生信息
SELECT * from student where age = (
select age from student WHERE sname = '陈樱'
)

2.in

#查询课程成绩不及格的选修课课程信息
select * from course where cno in(
select distinct cno from sc where degree < 60
)

#in或者not in 一般来说查询效率低,采用多表连接
select c.* from course c join sc on c.cno = sc.cno
where sc.degree < 60

3.all

#ALL表示必须满足子查询结果的所有记录
#查询sc表里成绩最高的记录
select * from sc where degree >= all (SELECT degree from sc)

#查询sc表里成绩最低的记录
select * from sc where degree <= all (SELECT degree from sc)

4.any

#any表示满足子查询结果的任意一条记录即可,和some一样
#查询选择'c01课程的插件高于'c02'的成绩的学生的学号
select * from sc where cno = 'c01' and degree >
ANY(select degree from sc where cno = 'c02')

select * from sc where cno = 'c01' and degree >
some(select degree from sc where cno = 'c02')

5.EXISTS

#显示已经选修了课程的学生信息
#多表查询
select distinct s.* from sc left join student  s on sc.sno = s.sno

#exists查询
select * from student s where exists (select distinct s.* from sc where sc.sno = s.sno)

#没有选修的同学
select * from student s where not exists (select distinct s.* from sc where sc.sno = s.sno)

#查询选修了c03课程的同学
select * from student s where exists (select distinct s.* from sc where sc.sno = s.sno and sc.cno = 'c03')

配套视频:0-查询篇-课程导读_哔哩哔哩_bilibili

sql文件:百度网盘 请输入提取码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冥王丁B

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值