oracle如何查询第几行到第几行的数据?(mysql的limit功能)。

转自:http://blog.sina.com.cn/s/blog_67e2758d0100s3oc.html

oracle实现"limit"功能

 

oracle数据库不支持mysql中limit功能,但可以通过rownum来限制返回的结果集的行数,rownum并不是用户添加的字段,而是oracle系统自动添加的。

(1)使查询结果最多返回前10行:

select * from OB_CALL_DATA_LOG where rownum<=10;

(2)使查询结果返回中间的10到100行:

如:     select * from OB_CALL_DATA_LOG rownum<101  minus  select * from OB_CALL_DATA_LOG rownum>9

注:select * from OB_CALL_DATA_LOG    and rownum>99 and rownum<101是错误的,oracle会认为条件不成立


再如:

 
select

 b.jgmc


from T_COMMUNITY_SERVICE t
left join T_CORRECT_ARCHIVE a
  on a.pk_id=t.A_PK
  inner join table(GET_CHILD_DEPT(579)) b
  on b.PK_ID = a.DEPT_PK
 where 
t.DR=0 and b.DR=0 and a.DR=0
      
    and rownum<10  minus 


select  

b.jgmc


 from T_COMMUNITY_SERVICE t
left join T_CORRECT_ARCHIVE a
  on a.pk_id=t.A_PK
  inner join table(GET_CHILD_DEPT(579)) b
  on b.PK_ID = a.DEPT_PK
 where t.DR=0 and b.DR=0 and a.DR=0
      
      and rownum>2
   
;