數據庫復習總結(15)-子查詢(分頁)
阿新 • • 發佈:2018-01-02
des play core splay 出現 opened b2c lose ont
子查詢:
(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 fromView CodeScoreInfo
--查詢參與了考試的學生信息 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()View Codeover(order by sid desc) as rowIndex from StudentInfo
(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 (View Codeselect *, ROW_NUMBER() over(order by sid desc) as rowIndex from StudentInfo) as t1 where rowindex between 5 and 8
數據庫復習總結(15)-子查詢(分頁)