高级查询---嵌套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