Oracle和SQL 2008分页查询

本文介绍了在Oracle和SQL Server数据库中实现分页查询的方法。对于Oracle,无论是单表还是多表查询,都通过ROWNUM进行记录编号,并结合WHERE子句实现分页。在SQL Server 2008中,则使用ROW_NUMBER()函数为每条记录分配唯一编号,再通过WHERE子句完成分页。这些技巧有助于提高数据库查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oralce分页查询

对于单表查询:

select *
from (select temp.*,rownum rn
      from (select * from t_ap_ap_app) temp)
where rn between 21 and 40;

select rn,temp1.*
from (select temp.*, rownum rn
      from (select * from t_ap_ap_app order by app_code desc) temp
      where rownum<=40) temp1
where rn>=21;

对于多表查询:

select *
from (select temp.*, rownum rn
      from (select app.app_code as app_code, app.app_status as status
            from t_tp_app app left join t_tp_app_line appLine
            on app.app_code=appLine.app_code) temp
      where rownum<=40)
where rn>=21;


SQL 2008分页查询

对于单表查询:

select * 
from (select ROW_NUMBER() over(order by temp.code desc) as rownum,temp.*
      from (select * from T_AP_ORDER) as temp) as temp1 
where temp1.rownum between 21 and 40 ;

对于多表查询:

select * 
from(select row_number() over(order by temp.code desc) as rownum,temp.* 
from (select od.CODE as code,od.STATUS as status
      from T_AP_ORDER as od left join T_AP_ORDER_LINE as odLine 
      on od.ID=odLine.ORDER_ID) as temp) temp1 
where rownum between 21 and 40;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值