MySQL 数据库 高级子查询的细节问题

1、查询多个符合条件的结果:

in

示例:查询所有《MySQL 数据库》 考试成绩刚好等于90分的学生名单

select studentName from studentInfo where studentID in (select studentID from exam where exam =90 and subjectID =(select subjectID from subject where subjectName ='MySQL数据库'));

not in

示例:查询所有《MySQL数据库》考试成绩刚好不等于90分的学生名单

select studentName from studentInfo where studentID not in ((select studentID from exam where exam=90 and subjectID =(select subjectID from subject where subjectName ='MySQL暑数据库')));

2、对子查询结果进行甄别,有结果返回true,没结果返回false 并且外层语句将不再进行剩余的查询操作

exists

示例:查询成绩表中科目编号为2的考试成绩中是否存在大于50的学生,如果存在就显示所有参加科目编号为2的考试学生信息

select studentID,exam from exam where subjectID=2 and exists(select studentID from exam where exam>50);

not exists

示例:查询成绩表中科目编号为4的考试成绩中是否存在不及格的学生,如果不存在就显示所有参加科目编号为4的考试学生信息

select studentID ,exam from exam where subjectID=4 and not exists(select studentID from exam where exam<60 and subjectID=4);

3.all、any/some的查询:

示例:查询成绩比科目编号为“1"的这门课程的所有成绩都大的学生考试成绩

select * from exam where exam>all (select exam from exam where subjectID=1);

查询成绩比科目编号为”1“ 的这门课程的任意一个成绩都大的学生考试成绩

select * from exam where exam>any (select exam from exam where subjectID=1);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值