您的当前位置:首页正文

数据库分页学习

2023-11-08 来源:化拓教育网

2.DB2数据库分页 Select * from (select rownumber() over() as rc,a.* from (select * from 表名 order by 列名) as a) where rc between startrow and endrow

3.SQL Server 2000数据库分页 Select top pagesize * from 表名 where 列名 not in(select top pagesize*page 列名 from 表名 order by 列名) order by 列名

4.SQL Server 2005数据库分页 Select * from (select 列名,row_搜索number() over(order by 列名1) as 别名from 表名) as t where t.列名1>=startrow and t.列名1<=endrow

5.MySQL数据库分页 Select * from 表名 limit startrow,pagesize (Pagesize为每页显示的记录条数)

6.PostgreSQL数据库分页 Select * from 表名 limit pagesize,offset startrow (Pagesize为每页显示的记录条数.)

7.通用模式select * from ( select * from tb_student where sid not in( select sid from tb_student where rownum<=(currentPage-1)*pageSize) ) where rownum <=pageSize;

另外,在项目中也可以使用Spring管理ibatis的Ioc容器SqlMapClientFactoryBean实现分页功能,例如:

public class ObjCheckDaoImpl extends SqlMapClientDaoSupport implements IObjCheckDao {
 /* * (non-Javadoc) * @see com.ailk.bomc.report.check.dao.IObjCheckDao#queryObjCheckInfo(com.ailk.bomc.report.check.bean.ObjCheckBean) */public List<ObjCheckBean> queryObjCheckInfo(ObjCheckBean objCheckBean, int page, int limit){return (List<ObjCheckBean>)this.getSqlMapClientTemplate().queryForList("objCheck.queryObjCheck", objCheckBean, (page-1)*limit, limit);}
}

数据库分页学习

标签:

小编还为您整理了以下内容,可能对您也有帮助:

SQL数据库分页原理(sql分页)

要想分页,首先得做好准备工作。你要先声明每页显示多少条数据,还得获取当前选择的是多少页的页码。有了这两个分页就好办了。

sql如下:selecttop10fromtableName

where(idnotin(selecttop20fromtableNameorderbyIddesc))orderbyIddesc

每页显示的数量:自己定义。

总页数:数据总条数/每页显示的条数

当前页码的计算方法:(页码-1)*每页显示的数量。比如我要浏览第3页的数据,3从客户端传送过来后,在后台对页码进行处理:(3-1)*每页显示的数量(假如是10).算出来后的结果就是20.你在把20以参数注入的方式动态添加到上面那个20那里就ok了。

sql中的10表示你每页显示的数据,这里跟10,就代表每页显示10条。(你可以定义一个常量作为每页显示的条数)

where中的20表示不包括前面的20条数据,也就是查询出从第21条到30之间的数据。

不知道我这样说你是否理解,其实只要理解了sql语句,分页就很好做了。

结合MySQL数据库,如何实现分页功能

第一步:我们可以利用$_GET方法获取某一个参数的值,在用户点击上一页、下一页、首页或者末页时获取不同的动态参数

第二步:根据MySQL的limit关键字,对动态参数进行SQL语句拼接

将定义每页显示的数据条数,在limit第一个参数的位置中,根据地址栏参数的动态变化从而实现指定从第几条数据开始显示数据

MySQL数据库limit分页、排序-SQL语句示例

MySQL数据库limit分页、排序-SQL语句示例

select*frompersonslimitA,B;

解释:

A,查询起点

B,你需要的行数

示例:

select*frompersonslimit0,4;

解释:

起点位置为0,开始查询,返回4条数据

select*frompersonslimit4,4;

解释:

起点为4,开始查询,返回4天数据。

特殊:

select*frompersonslimit10;

意思是,起点为0,开始查询,返回10条记录。

与select*frompersonslimit0,10;是等价的。

按规则排序的同时,进行分页:

select*frompersons

orderbylastname

limit0,10;

利用SQL语句对不同数据库进行高效果分页[2]

Oracle数据库

因为Oracle数据库没有Top关键字 所以这里就不能够像微软的数据据那样操作 这里有两种方法

( ) 一种是利用相反的

PAGESIZE 每页显示的记录数  CURRENTPAGE 当前页号

数据表的名字是 ponents  索引主键字是 id

select * from ponents where id not  in(select id from ponents where  rownum<=(PAGESIZE*(CURRENTPAGE )))  and rownum<=PAGESIZE order by id;

如下例

select * from ponents where id not in  (select id from ponents where rownum<= )  and rownum<= order by id;

从 到记录开始选择 选择前面 条

( ) 使用minus 即中文的意思就是减去

select * from ponents where rownum  <=(PAGESIZE*(CURRENTPAGE )) minus  select * from ponents where rownum  <=(PAGESIZE*(CURRENTPAGE ));  如例 select * from ponents where  rownum<= minus select * from ponents  where rownum<= ;

( ) 一种是利用Oracle的rownum 这个是Oracle查询自动返回的序号 一般不显示 但是可以通过select rownum from [表名]看到 注意 它是从 到当前的记录总数

select * from (select rownum tid ponents   * from ponents where rownum<= ) where tid<= ;

lishixin/Article/program/SQL/201311/16223

    利用SQL语句对不同数据库进行高效果分页[2]

    Oracle数据库

    因为Oracle数据库没有Top关键字 所以这里就不能够像微软的数据据那样操作 这里有两种方法

    ( ) 一种是利用相反的

    PAGESIZE 每页显示的记录数  CURRENTPAGE 当前页号

    数据表的名字是 ponents  索引主键字是 id

    select * from ponents where id not  in(select id from ponents where  rownum<=(PAGESIZE*(CURRENTPAGE )))  and rownum<=PAGESIZE order by id;

    如下例

    select * from ponents where id not in  (select id from ponents where rownum<= )  and rownum<= order by id;

    从 到记录开始选择 选择前面 条

    ( ) 使用minus 即中文的意思就是减去

    select * from ponents where rownum  <=(PAGESIZE*(CURRENTPAGE )) minus  select * from ponents where rownum  <=(PAGESIZE*(CURRENTPAGE ));  如例 select * from ponents where  rownum<= minus select * from ponents  where rownum<= ;

    ( ) 一种是利用Oracle的rownum 这个是Oracle查询自动返回的序号 一般不显示 但是可以通过select rownum from [表名]看到 注意 它是从 到当前的记录总数

    select * from (select rownum tid ponents   * from ponents where rownum<= ) where tid<= ;

    lishixin/Article/program/SQL/201311/16223

      java中数据库中实现分页的sql语句要求每页十条要查询的是第二页

      1、首先preparedstatement是statement的子接口,属于预处理操作,与直接使用statement不同的是,preparedstatement在操作的时候,先在数据表中准备好了一条sql语句,但是sql语句的值暂时不设置,而是之后设置。

      2、在使用statement的时候,要执行一条完整的失去了,在执行钱使用connection直接创建的。

      3、如何获得preparedstatement,在connection接口中,通过preparedstatement(String sql)得到。

      4、最后在日期输入的时候,正常情况都是使用java.util.date表示日期,在 preparedStatement中需要使用java.sql.date类型,如下图所示就完成了。

      几种常见SQL分页方式

      create table pagetest
      (
      id int identity(1,1) not null,
      col01 int null,
      col02 nvarchar(50) null,
      col03 datetime null
      )

      --分页1,not in/top
      select top 50 * from pagetest
      where id not in (select top 9900 id from pagetest order by id)
      order by id
       
      --分页2,not exists
      select top 50 * from pagetest
      where not exists
      (select 1 from (select top 9900 id from pagetest order by id)a  where a.id=pagetest.id)
      order by id
       
      --写法3,max/top
      select top 50 * from pagetest
      where id>(select max(id) from (select top 9900 id from pagetest order by id)a)
      order by id
       
      --分页4,row_number()
      select top 50 * from
      (select row_number()over(order by id)rownumber,* from pagetest)a
      where rownumber>9900
       
      select * from
      (select row_number()over(order by id)rownumber,* from pagetest)a
      where rownumber>9900 and rownumber<9951
       
      select * from
      (select row_number()over(order by id)rownumber,* from pagetest)a
      where rownumber between 9901 and 9950
       
      --分页5,在csdn上一帖子看到的,row_number() 变体,不基于已有字段产生记录序号,先按条件筛选以及排好序,再在结果集上给一常量列用于产生记录序号
      select *
      from (
          select row_number()over(order by tempColumn)rownumber,*
          from (select top 9950 tempColumn=0,* from pagetest where 1=1 order by id)a
      )b
      where rownumber>9900

      结论:

      1.max/top,ROW_NUMBER()都是比较不错的分页方法。相比ROW_NUMBER()只支持sql2005及以上版本,max/top有更好的可移植性,能同时适用于sql2000,access。

      2.not exists感觉是要比not in效率高一点点。

      3.ROW_NUMBER()的3种不同写法效率看起来差不多。

      4.ROW_NUMBER() 的变体基于这个测试效率实在不好。

      几种常见SQL分页方式

      create table pagetest
      (
      id int identity(1,1) not null,
      col01 int null,
      col02 nvarchar(50) null,
      col03 datetime null
      )

      --分页1,not in/top
      select top 50 * from pagetest
      where id not in (select top 9900 id from pagetest order by id)
      order by id
       
      --分页2,not exists
      select top 50 * from pagetest
      where not exists
      (select 1 from (select top 9900 id from pagetest order by id)a  where a.id=pagetest.id)
      order by id
       
      --写法3,max/top
      select top 50 * from pagetest
      where id>(select max(id) from (select top 9900 id from pagetest order by id)a)
      order by id
       
      --分页4,row_number()
      select top 50 * from
      (select row_number()over(order by id)rownumber,* from pagetest)a
      where rownumber>9900
       
      select * from
      (select row_number()over(order by id)rownumber,* from pagetest)a
      where rownumber>9900 and rownumber<9951
       
      select * from
      (select row_number()over(order by id)rownumber,* from pagetest)a
      where rownumber between 9901 and 9950
       
      --分页5,在csdn上一帖子看到的,row_number() 变体,不基于已有字段产生记录序号,先按条件筛选以及排好序,再在结果集上给一常量列用于产生记录序号
      select *
      from (
          select row_number()over(order by tempColumn)rownumber,*
          from (select top 9950 tempColumn=0,* from pagetest where 1=1 order by id)a
      )b
      where rownumber>9900

      结论:

      1.max/top,ROW_NUMBER()都是比较不错的分页方法。相比ROW_NUMBER()只支持sql2005及以上版本,max/top有更好的可移植性,能同时适用于sql2000,access。

      2.not exists感觉是要比not in效率高一点点。

      3.ROW_NUMBER()的3种不同写法效率看起来差不多。

      4.ROW_NUMBER() 的变体基于这个测试效率实在不好。

      C#中如何在数据库中提取数据进行分页?

      告诉你步骤吧:

      第一:在数据库中统计conut数,然后就能知道有多少条记录,然后除以每页最大显示条数,就能得到有多少页了。

      第二:使用Sql语句进行分页操作,比如有表Users,主键是UID,因此SQL语句如下:

      select top () * from Users where UID not in (select top () UID from Users)

      两个top后面的()是数字,第一个是当前要显示多少条记录,然后第二个是当前第几页,是通过计算得到,为最大显示条数 * 当前页数即可~

      最后得到的数据显示在页面上即可~

      Mysql 数据库怎么实现分页,要说的通俗一点儿

      一个不带limit 一个带limit。以php+mysql为例首先,连接数据库,写一条sql语句把你要查询的信息总量查找出来sql = select count(*) from tb,$all_page ;设定每页显示条数, $display 。然后,当前页为$page ;在写一句sql = select * from tb limit $dispaly*($page - 1),$display;最后,在页面显示分页信息把当前页传回给分页处理页,一定要把相关的条件一起传回去,get 方式传值,否则查询条件改变查询信息就不正确。 网上有好多封装好的分页类。我也有一个很好用的分页类,如果请我吃肉就发给你一份哈。。\(^o^)/~ 追问: 这个$all_page用在哪儿,怎么将当前页传回给分页处理页。显示的时候那些“首页”“上一页”“下一页”“末页”是链接吗、链到什么地方,还是别的什么 回答: $all_page是查询总数,总是页数等于查询总数除以每页显示的信息。$num_page = ceil($all_page/$display); 用get方式把当前页传给分页处理页,就是<a href = "连接到本页或着不写也就是当前页?page=当前页码"></a>标签 别的我也想不起来,让我自己写分页,我只会最简单的那种,一般我都是调用一个现成的分页类。只需传个参数就Ok,连样式都不用写的。。。