首页 > 数据库技术 > 详细

用一条SQL语句 查询出每门课程都大于80分的学生姓名" 的实现方式

时间:2015-07-03 15:22:37      阅读:390      评论:0      收藏:0      [点我收藏+]

建表:

 1 create table Score(
 2 name varchar(20),
 3 course varchar(20),    
 4 score int)
 5 
 6 go
 7 
 8 insert Score 
 9 select ‘HanMeimei‘,‘Chinese‘,81 union all
10 select ‘HanMeimei‘,‘Mathematics‘,75 union all 
11 select ‘HanMeimei‘,‘History‘,81 union all 
12 select ‘HanMeimei‘,‘Geography‘,75 union all 
13 select ‘LiLei‘,‘Chinese‘,88 union all 
14 select ‘LiLei‘,‘Mathematics‘,90 union all 
15 select ‘JimGreen‘,‘Chinese‘,81 union all 
16 select ‘JimGreen‘,‘Mathematics‘,100 union all 
17 select ‘JimGreen‘,‘English‘,90

 

方法1:  找出有科目没有达到80分的姓名,然后过滤

1 select distinct name 
2 from score a 
3 where not exists(select 1 from score where a.name=name and score<80)
1 select distinct name 
2 from score 
3 where name not in (select name from score where score<80)

 

方法2:  通过分组后过滤的方式

1 select name
2 from score 
3 group by name 
4 having COUNT(1)=SUM(case when score>=80 then 1 else 0 end)
1 select name 
2 from score
3 group by name 
4 having MIN(score)>=80

 

结果:

name

JimGreen
LiLei

 

在记录较少(9条)的情总下  以上四种方式的执行计划(SQL2008)用时比例为

14:14:11:11

 

用一条SQL语句 查询出每门课程都大于80分的学生姓名" 的实现方式

原文:http://www.cnblogs.com/hjqc/p/4618597.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!