分页查询sql

常用分页查询sql

先介绍一个面试题,查询表中第200-300条数据。应用既是分页查询,先通过子查询对数据进行标记,如oracle通过rownum进行标记,再取一个区间的数据。

一,oracle

关键字 rownum

规则:  

 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=startrow

或 select *
  from (select a.*, rownum rn
          from (select * from table_name) a
         where rownum <= 40)
 where rn > 20

 

其中的select * from table_name 表示没有进行分页时的sql查询语句,而rownum 和rn 限定了查询数据的范围。

实例:

   select * from ( select ol.*, rownum  rc from offsiteoutreachteam_list ol where rownum <= 48 ) ol where ol.rc >= 9      

   select * from (select a.*, rownum rn from (select * from offsiteoutreachteam_list) a where rownum <= 40) where rn > 20      

  select * from ( select ol.*, rownum rc from offsiteoutreachteam_list ol) where rc >= 9 and rc <= 20

  select * from ( select ol.*, rownum rc from offsiteoutreachteam_list ol) where rc between 10 and 20

  注意:可以通过排序,避免不同页出现重复的数据。

  1.   select * from  
  2.   2  (  
  3.   3  select a.*, rownum rn  
  4.   4  from (select id, owner, object_name from test where owner is not null order by owner, id) a  
  5.   5  where rownum <= 20  
  6.   6  )  
  7.   7  where rn >= 11;

 

二,mysql

关键字:limit

mysql数据库分页     select * from 表名 limit startrow,pagesize     (pagesize为每页显示的记录条数)    

三,sql servel

关键字:top

   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    

 

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐