数据库复习总结(15)-子查询(分页)

Posted mhq-martin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库复习总结(15)-子查询(分页)相关的知识,希望对你有一定的参考价值。

子查询:

(1)将一个查询语句嵌入另一个查询语句中,称这种查询为子查询
(2)出现在条件部分常用的运算符:= 、in 、exists(exists和in效果相同,但是exists效率高些)
(3)分页:已知页大小、页索引,查询出当前页的数据
提示1:排名函数row_number(),结合开窗函数over(order by 列名)进行排序号
提示2:between ... and ...

技术分享图片

例1:(in和exists)

技术分享图片
use  dbtest
select * from StudentInfo 
select * from ScoreInfo
select stuid from ScoreInfo
View Code

技术分享图片

技术分享图片
--查询参与了考试的学生信息 exists in
select * from StudentInfo
where sId in(select distinct stuid from ScoreInfo)

select * from StudentInfo
where exists 
(select * from ScoreInfo where ScoreInfo.stuId=StudentInfo.sid)
View Code

技术分享图片

2、分页

(1)提供索引

技术分享图片
select *,
ROW_NUMBER() over(order by sid desc) as rowIndex
from StudentInfo
View Code

技术分享图片

(2)举例子

技术分享图片
--分页 已知:页大小(一页显示多少条数据),页索引
--            3,4                        1,2,3,4
--1,3   1,3        (pageIndex-1)*pageSize+1    pageIndex*pageSize
--2,3    4,6
--3,3    7,9
--4,3    10,12

--2,4    5,8
select * from
(select *,
ROW_NUMBER() over(order by sid desc) as rowIndex
from StudentInfo) as t1
where rowindex between 5 and 8
View Code

技术分享图片

 

以上是关于数据库复习总结(15)-子查询(分页)的主要内容,如果未能解决你的问题,请参考以下文章

高级查询及分页总结

[转]史上最全的MSSQL复习笔记

MySQL学习笔记连接子分页联合查询以及sql语句执行顺序总结

CGBTN2109-DAY15总结复习

JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询

JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询