高级查询---嵌套and分页

高级嵌套语句:

子查询: 

语句:

select * from 表名

where 列名=

子查询语句

注意:子查询语句必须放在小括号呢 可以使用< >=等运算符号,sql server 执行时 会优先先进行子查询 

因为子查询作为where条件的一部分 所以 可以和 update insert delete 一起使用

 

in 和 not in子查询:

当使用< > =等运算符号进行子查询时 要求 返回值不能 大于1,所以 int 和 not in 的子查询就避开了这一限制 

举个列子  要查询没有学员参加oop考试的学员名字

--查询没有参加oop的学员名字
select studentname
from student
where StudentNo not in
(
select StudentNo
from Result
where ExamDate=
(
select MAX(ExamDate)from Result
where SubjectId=
(
select SubjectId from Subject
where SubjectName='oop'
)
)

可以很 明显的看出 所谓 的 int  not int子查询 只是把where条件的运算符号 改为了 in 或 not int 。

 

 

分页查询:

分页查询分为两种:

1用双 top 和 双 order by 进行分页 

select top 4 *from student
where studentName not in
(
select top 4 StudentName from Student
order by StudentName
)
order by StudentName

重点:外层先要确定好要显示几行数据,内层要选择从几到几进行分页

 

 

2. --在表中 构建一列 并按序号排列(别名列)用别名列进行分页

select * from
(
select *,ROW_NUMBER() over (order by studentno) as Myid
from Student
)
as temp
where Myid between 3 and 4

 

转载于:https://www.cnblogs.com/hero96/p/5259550.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值