zoukankan      html  css  js  c++  java
  • 数据库复习总结(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

  • 相关阅读:
    POJ 1466 最大独立点集
    POJ 3159 差分约束
    POJ 3411 DFS
    POJ 2665 模拟,,
    POJ 3134 Power Calculus ID-DFS +剪枝
    POJ 1543 暴搜
    455. Assign Cookies
    715. Range Module
    530. Minimum Absolute Difference in BST
    493. Reverse Pairs(BST, BIT, MergeSort)
  • 原文地址:https://www.cnblogs.com/mhq-martin/p/8150583.html
Copyright © 2011-2022 走看看