• 数据库复习总结(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

  • 相关阅读:
    简单实现 C# 与 Javascript的兼容
    如何写好CSS系列之表单(form)
    D3、openlayers的一次尝试
    如何写好css系列之button
    mockjs,json-server一起搭建前端通用的数据模拟框架
    AIX中的/etc/inittab文件
    AIX中crontab和at 定时任务
    AIX中的服务管理
    AIX系统的备份和恢复
    AIX中磁带设备的使用
  • 原文地址:https://www.cnblogs.com/mhq-martin/p/8150583.html
Copyright © 2020-2023  润新知