zoukankan      html  css  js  c++  java
  • oracle和mysql分页

    mysql分页

    关键字limit,limit m,n 其中m表示起始位置的下标,下标从0开始。n表示要显示的条数,比如要查询一个表的第2到5条数据。

    select * from emp limit 1,4;

    oracle分页

    关键字rownum, rownum表示返回数据的行号。使用它需谨慎,不能用rownum大于(大于1的数值)、大于等于(大于或等于1的数值)、=(大于或等于1的数值),否则无结果。

    select count(*) from gcfr_t_vch a where rownum>1;

    运行结果如下:

    实际上这个表里是有数据的,理论上应该是能查出来数据的。但是为什么没有出来呢?

    这是因为:

    1、ROWNUM是伪列,必须要要有返回结果后,每条返回记录就会对应产生一个ROWNUM数值;

    2、返回结果记录的ROWNUM是从1开始排序的,因此第一条始终是1;

     这样,当查询到第一条记录时,该记录的ROWNUM为1,但条件要求ROWNUM>1,因此不符合,继续查询下一条;因为前面没有符合要求的记录,因此下一条记录过来后,其ROWNUM还是为1,如此循环,就不会产生结果。上述查询可以通过子查询来替代:

     select count(*) from ( select guid,rownum r from gcfr_t_vch t ) tt where tt.r >1

    所以使用rownum进行分页,把rownum转成一个普通列,然后再利用运算关系计算即可,比如查询gcfr_t_vch表的第3到4条数据。

    select * from (select rownum r,t.* from gcfr_t_vch t) tt where tt.r >= 3 and tt.r <= 4;
    select * from (select rownum r,t.* from gcfr_t_vch t) tt where tt.r between 3 and 4;
    
    select * from (select rownum r,t.* from gcfr_t_vch t) tt where  tt.r <= 4
    minus
    select * from (select rownum r,t.* from gcfr_t_vch t) tt where  tt.r <= 2;

    以上3种写法都可以。另外有个公式,用的时候直接套就行了。

    select * from (select rownum r,t.* from gcfr_t_vch t) tt where tt.r >((pageNo - 1) * pageSize) and tt.r <= (pageNo * pageSize);

    其中pageNo表示要显示第几页,pageSize表示每页要显示的条数。

  • 相关阅读:
    设计模式之观察者模式
    设计模式之模板方法模式
    设计模式之代理模式(全面讲解)
    设计模式之工厂模式(包含三种模式)
    设计模式之单例模式(包含三种方式)
    opencv+vs2015 堆内存析构异常
    用python来压缩文件
    GPT安装ubuntu的问题
    Two Sum and add Two Numbers
    [LeetCode] Palindrome Partitioning II
  • 原文地址:https://www.cnblogs.com/hkdpp/p/8327857.html
Copyright © 2011-2022 走看看