SQL高级查询:嵌套和分页
Posted 美味的你
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL高级查询:嵌套和分页相关的知识,希望对你有一定的参考价值。
1.嵌套子查询
--查询最近一次oop考试没有参加考试的学生
select StudentName from Student where StudentNo not in( select StudentNo from Result where SubjectId=( select SubjectId from Subject where SubjectName=‘oop‘ ) and ExamDate=( select MAX(ExamDate) from Result where SubjectId= (select SubjectId from Subject where SubjectName=‘oop‘ ) ) )
2.相关子查询
SElECT name,id,price FROM commodity As c WHERE price> ( SELECT AVG(price) FROM commodity AS a WHERE c.类编号=a.类编号 )
3.分页查询
第一种:
--跳过五行取两行 select top 2 * from Student where StudentNo not in ( select top 5 StudentNo from Student order by StudentNo ) order by StudentNo
第二种:
--取第四行到第六行 select * from (select *,ROW_NUMBER() over(order by StudentNo) as myid from Student) as temp where myid between 4 and 6
以上是关于SQL高级查询:嵌套和分页的主要内容,如果未能解决你的问题,请参考以下文章