sql server 高级查询
--in和not in子查询
--采用in子查询获得参加考试的在读学生名单
select StudentId,StudentName from Student where StudentId in
(
select StudentId from Result
where subjectId=(
--获得参加java考试最近一次考试的学生学号
select subjectId from Subject
where SubjectName='java'
)and ExamDate=(
--获得java课程最近一次的考试日期
select max(ExamDate) from Result
where subjectId=(
--获得java课程的课程编号
select subjectId from Subject
where SubjectName='java'
)
)
)
--采用not in子查询,查看未参加考试的在读学生名单
select StudentId,StudentName from Student
where StudentId not in(
select StudentId from Result
where subjectId=(
select subjectId from Subject
where SubjectName='java'
)
and ExamDate=(
select max(ExamDate) from Result
where subjectId=(
select subjectId from Subject
where SubjectName='java'
)
)
)
--分页查询
select top 2 * from Student
where StudentId not in
(
select top 2 StudentId from Student
)
--exists子查询
--采用exists子查询,进行酌情加分
if exists(
--查询java课程最近一次考试成绩大于80分的记录
select * from Result where subjectId=(
select subjectId from Subject where SubjectName='java'
)and ExamDate=(
select max(ExamDate)from Result where subjectId=(
select subjectId from Subject
where SubjectName='java')
)and StudentResult>80
)
--如果存在考试成绩高于80分的学生,则参加本次考试的学生每人加2分
--加分前的最高成绩不得高于98分
begin
update Result set StudentResult=StudentResult+2
where subjectId=(
select subjectId from Subject where SubjectName='java'
)and ExamDate=(
select max(ExamDate) from Result where subjectId=(
select subjectId from Subject
where SubjectName='java')
)and StudentResult<=98
end
else
--如果考试成绩都低于80分,则参加考试的学生每人加5分
begin
update Result set StudentResult=StudentResult+5
where subjectId=(
select subjectId from Subject where SubjectName='java'
)and ExamDate=(
select max(ExamDate) from Result where subjectId=(
select subjectId from Subject
where SubjectName='java')
)
end
sql server 高级查询的更多相关文章
- SQL Server高级查询
简介 关于数据库,我们经常会听说"增查删改"之类的词语,听起来很简单,但是如果想要准确的获取到需要的数据的话,还是要花点功夫的.下面由我来和大家谈谈高级查询的用法以及和普通查询的区 ...
- 转载 50种方法优化SQL Server数据库查询
原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...
- Sql Server中查询今天、昨天、本周、上周、本月、上月数据
Sql Server中查询今天.昨天.本周.上周.本月.上月数据 在做Sql Server开发的时候有时需要获取表中今天.昨天.本周.上周.本月.上月等数据,这时候就需要使用DATEDIFF()函数及 ...
- Sql Server参数化查询之where in和like实现详解
where in 的参数化查询实现 首先说一下我们常用的办法,直接拼SQL实现,一般情况下都能满足需要 string userIds = "1,2,3,4"; using (Sql ...
- 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参
转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...
- 【转】Sql Server参数化查询之where in和like实现详解
转载至:http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 文章导读 拼SQL实现where in查询 使用CHARINDE ...
- SQL Server中查询用户的对象权限和角色的方法
--SQL Server中查询用户的对象权限和角色的方法 -- 查询用户的object权限 exec sp_helprotect NULL, 'sa' -- 查询用户拥有的role exec sp_h ...
- 优化SQL Server数据库查询方法
SQL Server数据库查询速度慢的原因有很多,常见的有以下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列 ...
- SQL Server 2008 查询所有用户表
SQL Server 2008 查询所有用户表的T-SQL语句是: SELECT * FROM sysobjects WHERE [xtype] = 'U' 或者是: SELECT * FROM sy ...
随机推荐
- 解决beego中同时开启http和https时,https端口占用问题
在beego的app.go文件中, 找到 // run normal mode if BConfig.Listen.EnableHTTPS { go func() { time.Sleep( * ti ...
- Centos7上实现不同网段的服务器文件共享
目的:实现不同网段的服务器实现文件共享 前提:服务器1可以和共享服务器互通,共享服务器和服务器2互通 拓扑如下: 思路: 一般文件共享有涉及windown系统的用samba,纯类centos系统就用n ...
- Python学生信息管理系统的开发
# 第一题:设计一个全局变量,来保存很多个学生信息:学生(学号, 姓名,年龄):思考要用怎样的结构来保存:# 第二题:在第一题基础上,完成:让用户输入一个新的学生信息(学号,姓名,年龄):你将其保存在 ...
- HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- C. cltt的幸运数LCAtarjan
/*C: cltt的幸运数 Time Limit: 1 s Memory Limit: 128 MB Submit Problem Description 一棵树有n个节点,共m次查询,查询 ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
- Vue2.0 探索之路——生命周期和钩子函数的一些理解
前言 在使用vue一个多礼拜后,感觉现在还停留在初级阶段,虽然知道怎么和后端做数据交互,但是对于mounted这个挂载还不是很清楚的.放大之,对vue的生命周期不甚了解.只知道简单的使用,而不知道为什 ...
- Elasticsearch索引模板和别名
创建模板(模板名和索引名一样都不能有大写) PUT http://222.108.x.x:9200/_template/templateds { "template": " ...
- [转] mongodb下载、安装、配置与使用
记得在管理员模式下运行CMD,否则服务将启动失败. 详细图解,记录 win7 64 安装mongo数据库的过程.安装的版本是 MongoDB-win32-x86_64-2008plus-ssl-3.4 ...
- 使用htpasswd及nginx auth模块对指定页面进行登录验证
某些时候,作为运维挂你人员会部署一些工具用于使用外网对内部服务器进行某些管理,比如phpmyadmin.gateone堡垒机等工具.但是这些软件 一旦部署之后,所有人都可以访问到我们的登录页面似乎并不 ...