MySQL学习笔记

本文探讨了SQL查询中如何通过嵌套查询找出最高分学生信息,同时区分了查询学生姓名和学号时是否需要明确指定表名。还展示了查找与特定学号选课相同的同学,以及针对班级内特定条件(如性别和朱凡同学)筛选同学的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

⑤1)查询和score表中最高分一样的学生信息【嵌套查询

select Stu_no from score where score=(select max(score) from score);

2)查询和score表中最高分一样的学生学号和学生姓名【嵌套查询:一个查询的条件来自于另一个查询的结果时可以使用嵌套查询】

select student.Stu_no,student.Stu_name from score join student on score.Stu_no=student.Stu_no where score=(select max(score) from score);

3)查询和1801010101选了同样课程的学生信息

select student.Stu_no,Stu_name from score join student on score.Stu_no=student.Stu_no where Course_no in(select Course_no from score where Stu_no='1801010101');【多个条件用in,仅一个条件用=】

问:两个标蓝的语句中,为什么第一个加了表student的名字,而第二个没有加?

因为这个语句是一个自然连接,由于stu.no是两个的关键字,关联了两张表,因此在两张表中存在stu.no,因此需要在此处标明是哪张表里的stu.no;

4、any表示大于一组值中任意一个值:>any(1,2,3)——大于1;

5、all表示大于一组值中每个值:>all(1,2,3)——大于3;

6、

1)#查询和朱凡在同一个班的其他同学并且是男生的信息

select * from  student where Stu_sex='男' and Class_no=(select Class_no from student where Stu_name='朱凡' );

2)#查询和朱凡在同一个班的其他同学的信息

select * from  student where Class_no=(select Class_no from student where Stu_name='朱凡' );

3)#查询和朱凡在同一个班的其他同学并且是男生的信息

select * from  student where Stu_sex='男' and Class_no=(select Class_no from student where Stu_name='朱凡' ) and Stu_name!='朱凡';【也可以写为”<>”,均表示为不等于】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值