用一条SQL语句 查询出每门课程都大于80分的学生姓名" 的实现方式
2015-07-03 14:36
801 查看
建表:
建立索引
方法1: 找出有科目没有达到80分的姓名,然后过滤
方法2: 通过分组后过滤的方式
在记录较少(9条)的情况下 以上四种方式的执行计划(SQL2008)用时比例约为
1:1:1:1
更新:
在记录数较多(49995条)的情况下,以上四种方式的执行计划(SQL2008)用时比例为
3:85:6:6
create table Score( name varchar(20), course varchar(20), score int) go insert Score select 'HanMeimei','Chinese',81 union all select 'HanMeimei','Mathematics',75 union all select 'HanMeimei','History',81 union all select 'HanMeimei','Geography',75 union all select 'LiLei','Chinese',88 union all select 'LiLei','Mathematics',90 union all select 'JimGreen','Chinese',81 union all select 'JimGreen','Mathematics',100 union all select 'JimGreen','English',90
建立索引
create nonclustered index UIX_Score_Score on score(score) include (name) create nonclustered index UIX_Score_Name on score(name) create nonclustered index UIX_Score_Course on score(course) include(name)
方法1: 找出有科目没有达到80分的姓名,然后过滤
select distinct name from score a where not exists(select 1 from score where a.name=name and score<80)
select distinct name from score where name not in (select name from score where score<80)
方法2: 通过分组后过滤的方式
select name from score group by name having COUNT(1)=SUM(case when score>=80 then 1 else 0 end)
select name from score group by name having MIN(score)>=80
在记录较少(9条)的情况下 以上四种方式的执行计划(SQL2008)用时比例约为
1:1:1:1
更新:
在记录数较多(49995条)的情况下,以上四种方式的执行计划(SQL2008)用时比例为
3:85:6:6
相关文章推荐
- Mybatis基于注解的方式访问数据库
- memcached client -- memcached client for java使用
- mongodb 更新数组出现can't append to array using string field name
- 在ASP.NET WebAPI 中使用缓存【Redis】
- mysql utf8编码设置
- MYSQL查询某字段中以逗号分隔的字符串的方法
- ERROR:duplicate key in table MYsql
- 【项目开发】mysql-5.5字符编码相关的设置与查询
- Mysql创建分区
- SQL语句大全教程
- T-Sql编程基础
- MongoDB学习笔记--分组(1)
- Oracle 11gR2 使用exp,imp命令导入与导出
- mysql主从复制
- Log4Net的应用教程之保存日志到数据库中
- 58 sql 添加/删除字段
- sql注入
- mysql常用查询语句
- 手把手教你搭建Oracle Secure Global Desktop演示环境
- SqlServer 垂直分表