--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 高级查询的更多相关文章

  1. SQL Server高级查询

    简介 关于数据库,我们经常会听说"增查删改"之类的词语,听起来很简单,但是如果想要准确的获取到需要的数据的话,还是要花点功夫的.下面由我来和大家谈谈高级查询的用法以及和普通查询的区 ...

  2. 转载 50种方法优化SQL Server数据库查询

    原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...

  3. Sql Server中查询今天、昨天、本周、上周、本月、上月数据

    Sql Server中查询今天.昨天.本周.上周.本月.上月数据 在做Sql Server开发的时候有时需要获取表中今天.昨天.本周.上周.本月.上月等数据,这时候就需要使用DATEDIFF()函数及 ...

  4. Sql Server参数化查询之where in和like实现详解

    where in 的参数化查询实现 首先说一下我们常用的办法,直接拼SQL实现,一般情况下都能满足需要 string userIds = "1,2,3,4"; using (Sql ...

  5. 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参

    转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...

  6. 【转】Sql Server参数化查询之where in和like实现详解

    转载至:http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 文章导读 拼SQL实现where in查询 使用CHARINDE ...

  7. SQL Server中查询用户的对象权限和角色的方法

    --SQL Server中查询用户的对象权限和角色的方法 -- 查询用户的object权限 exec sp_helprotect NULL, 'sa' -- 查询用户拥有的role exec sp_h ...

  8. 优化SQL Server数据库查询方法

    SQL Server数据库查询速度慢的原因有很多,常见的有以下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列 ...

  9. SQL Server 2008 查询所有用户表

    SQL Server 2008 查询所有用户表的T-SQL语句是: SELECT * FROM sysobjects WHERE [xtype] = 'U' 或者是: SELECT * FROM sy ...

随机推荐

  1. Axure实现多用户注册验证

    *****多用户登录验证***** 一.(常规想法)方法:工作量较大,做起来繁琐 1.当用户名和密码相同时怎么区分两者,使用冒号和括号来区分: eg. (admin:123456)(123456:de ...

  2. ajax--参数默认值问题

    注:通过参数默认值,能让参数映射更加灵活,有些参数可以不必传递,如果传递则 覆盖默认.并且永远都是后面的覆盖前面的内容 通过$.extend合并对象 语法1: var newobj= $.extend ...

  3. appium+python测试app使用相对坐标定位元素

    我们获取到的是绝对坐标,如果换一个屏幕分辨率不同的手机那这个坐标自然会发生变化,要实现不同手机均能实现点击同一控件自然要用到相对坐标了,具体方法如下: 1.获取当前空间的绝对坐标(x1,y1),开启指 ...

  4. ERP系统

    ERP系统是企业资源计划(Enterprise Resource Planning )的简称,是指建立在信息技术基础上,集信息技术与先进管理思想于一身,以系统化的管理思想,为企业员工及决策层提供决策手 ...

  5. openstack 基础

    一:openstack起源: 1.rackspace和NASA(美国国家航空航天局)共同发起的开源项目 1.1/rackspace:贡献的swaft子项目(存储组件) 1.2/NASA:贡献了nova ...

  6. urls.py路由系统分发的本质

    路由系统分发的本质 反向生成redirect跳转url 1.在urls.py中指定url的name from django.conf.urls import url from django.contr ...

  7. DNS解析中的A记录、AAAA记录、CNAME记录、MX记录、NS记录、TXT记录、SRV记录、URL转发等

    AA记录: 将域名指向一个IPv4地址(例如:100.100.100.100),需要增加A记录 NSNS记录: 域名解析服务器记录,如果要将子域名指定某个域名服务器来解析,需要设置NS记录 SOASO ...

  8. 无法连接ssh,fatal: daemon() failed: No such device

    今天发现一个服务器的sshd无法启动,查看/var/log/secure里发现:fatal: daemon() failed: No such device 解决办法: rm /dev/null /d ...

  9. Linux 的文件类型

    Linux 的文件通常分为 7 大类 文件类型                                  缩写      英文名称                                ...

  10. Hankson 的趣味题

    题解: 硬是把一道傻逼题写到了200行.. 长长的模板就有70行.. 由于我没有做的时候觉得并不保证$a1|a0$ $b0|b1$ 然后就加了很多特判.. 我的做法就是暴力分解质因数 T*sqrt(n ...