Mysql与Oracel的分页查询

本文介绍Mysql和Oracle数据库中实现分页查询的方法。Mysql通过LIMIT关键字指定起始位置和返回记录数;Oracle则利用ROWNUM进行复杂查询,包括通过多层嵌套查询实现精确的数据区间选取。

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

Mysql分页查询limit m,n

select t.* from test t
limit m, n
//m代表从那一行开始
//n代表查几条数据

查询表bbs_color中的第1-5条数据

SELECT
bbs_color.id,
bbs_color.`name`,
bbs_color.parent_id,
bbs_color.img_url
FROM
bbs_color
LIMIT 0, 5

结果
这里写图片描述
查询表bbs_color中的第6-10条数据

SELECT
bbs_color.id,
bbs_color.`name`,
bbs_color.parent_id,
bbs_color.img_url
FROM
bbs_color
LIMIT 5, 5

结果
这里写图片描述

Oracle分页查询rownum

  • 不能对rownum 使用 >(大于或等于1的数值)、>=(大于1的数值)、=(大于1的数值),否则无结果
  • rownum >10 没有记录,因为第一条不满足去掉的话,第二条的rownum又成了1,所以永远没有满足条件的记录
多条语句查询

这种查询语句效率较高,内层控制最大值,外层控制最小值.
- 查询poi表的第1-5条数据

select rownum rn ,t.* from poi t where rownum <= 5;
select t2.* from (select rownum rn ,t.* from poi t) t2 where rn <= 5;--前5条

结果
这里写图片描述

  • 查询poi表的第6-10条数据
select t3.* from (select rownum rn2,t2.* from (select rownum rn,t.* from poi t) t2 where rn <= 10) t3 where rn2 > 5; --6到10条
select t3.* from (select t2.* from (select rownum rn,t.* from poi t) t2 where rn <= 10) t3 where rn > 5; --6到10条
select t2.* from (select rownum rn,t.* from poi t where rownum <=10) t2 where rn > 5;
select t2.* from (select rownum rn,t.* from poi t where rownum <=10) t2 where rn >= 6;

结果
这里写图片描述

between m and n
select rownum rn,t.* from poi t where rownum between 1 and 5 ;
select t2.* from (select rownum rn,t.* from poi t) t2 where t2.rn between 1 and 5 ;
select t2.* from (select rownum rn,t.* from poi t) t2 where t2.rn between 6 and 10 ;

结果
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值