为了账号安全,请及时绑定邮箱和手机立即绑定

数据库知识点(5)——多列过滤

标签:
MySQL

多列过滤是在where语句后用and或or连接对列进行过滤的条件,从而筛选出符合条件的数据。

此篇以学生成绩为例进行演示。

涉及的表有student(学生信息表)和stuscore(成绩表)如图:
图片描述
1、查询语文优秀(85-100)的学生信息

使用关联查询

 select a.no,a.name,b.subject,b.score from student a,stuscore b  
where a.no = b.stuno and b.subject='语文' and( b.score between 85 and 100);

使用子查询

select a.no,a.name,b.subject,b.score from student a join stuscore b on a.no = b.stuno  where (stuno,score) in (select stuno,score from stuscore where b.subject='语文' and( b.score between 85 and 100) );

结果
图片描述
2、查询语文或英语有一科优秀的学生信息

 select a.no,a.name,b.subject,b.score from student a  
       join stuscore b on a.no = b.stuno  
       where (stuno,score) in
       (select stuno,score from stuscore
		 where (b.subject='语文' and( b.score between 85 and 100)) or  
		       (b.subject='英语' and( b.score between 85 and 100)))  ;

图片描述
3、查询语文和英语都优秀的学生信息

 select a.no,a.name,b.subject,b.score from student a  
       join stuscore b on a.no = b.stuno  
       where (stuno,score) in
       (select stuno,score from stuscore 
		where  (b.subject='语文' and( b.score between 85 and 100)) and 
		       (b.subject='英语' and( b.score between 85 and 100))) ;

图片描述

优先级是先括号中的内容,在and,在or

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
103
获赞与收藏
598

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消