美文网首页
ADO.Net学习笔记--子查询及分页的应用

ADO.Net学习笔记--子查询及分页的应用

作者: 丨Zenger丶 | 来源:发表于2018-02-14 15:44 被阅读16次
    子查询介绍

    独立子查询:

    select * from TblStudent where tSClassId = (select tClassId from TblClass where tClassName = '高二二班')

    子查询

    独立子查询:

    select * from TblStudent as ts where exists(select * from TblClass as tc where ts.tSClassId=tc.tClassId and tc.tClassName = '高二二班')

    所有查询都可以用相关子查询来代替

    where 列名 = 一个值

    where 列名 in 几个值

    ---------------------分页-------------

    --要求,每页显示10条  首先要确定排序

    --第一页

    select top (10*2) * from Customers order by CustomerID asc

    --要查询第2页思路:先查询前2-1的customerId

    select top 10 * from Customers where CustomerID not in (

    select top (10 * (2-1)) CustomerID from Customers order by CustomerID asc)

    order by CustomerID asc

    --查询最后一页数据,每页5条数据

    select top 5 * from Customers where CustomerID not in (

    select top (5 * (((select count(*) from Customers)/5))) CustomerID from Customers order by CustomerID asc)

    order by CustomerID asc

    select * from Customers order by CustomerID asc

    --查询最后一页数据,每页5条数据

    select* from Customers where CustomerID in(

    select top 10 CustomerID from Customers order by CustomerID desc) order by CustomerID asc

    --查询第一页数据,每页5条数据

    select top 10 * from Customers order by CustomerID asc

    ---分页另一种思路:使用Row_Number()函数------

    --使用row_nuber()实现

    --1、添加一列,专门为数据排序,然后编号

    select * ,RN = ROW_NUMBER()  over (order by CustomerID)from Customers

    --2、根据用户要查看的每页记录数,以及要查看第几页,确定应该查询第几条到第几条

    --查询第五页,每页8条

    select * from (select * ,RN = ROW_NUMBER()  over (order by CustomerID)from Customers ) as t where t.RN between (5-1)*8+1 and 5*8

    相关文章

      网友评论

          本文标题:ADO.Net学习笔记--子查询及分页的应用

          本文链接:https://www.haomeiwen.com/subject/snsetftx.html