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

  • 相关阅读:
    Java中断机制
    RPC原理
    synchronized和ReentrantLock的区别
    dubbo入门
    Zookeeper入门
    分布式事务
    Mysql索引会失效的几种情况
    java代码执行过慢的问题定位
    持续集成
    Mycat 数据库分库分表中间件
  • 原文地址:https://www.cnblogs.com/mhq-martin/p/8150583.html
Copyright © 2011-2022 走看看