oracle不支持mysql的limit功能,推荐使用fetch
一、MYSQL使用limit返回指定的行数
select * from table limit m,n; //从m+1行开始返回,返回n行 select * from table limit n; //相当于select * from table limit 0,n; select * from table limie m,-1; //从m+1行开始返回,返回至最后一行
1、从http://www.xuesql.cn/得到测试数据
b、select * from table limit m,n; 从m+1行开始返回,共返回n行
SELECT * FROM movies limit 5,5;
c、select * from table limit n;
SELECT * FROM movies limit 5;
d、select * from table m,-1;
SELECT * FROM movies limit 5,-1;
二、oracle使用rownum来返回指定的行数(rownum不是用户自定义的字段,是系统定义的伪列)或者offset/fetch新特性
select first_name,last_name,hire_date from employees; //共107行数据
employees表创建参考连接:https://www.cnblogs.com/muhai/p/16169598.html
1、rownum的使用(从1开始),比较复杂,推荐使用fetch
(1)、从头查询方式,不支持从中间查询
select employee_id,first_name,last_name,salary from employees where rownum<5; //返回前4行 select employee_id,first_name,last_name,salary from employees where rownum>5; //返回空,因为不支持>符号 select employee_id,first_name,last_name,salary from employees where rownum=1; //返回1行,如果m=1,返回第一行;m≠1就返回空
(2)、从中间查询方式,通过子查询返回第2行到第5行数据
select * from (select employee_id,first_name,last_name,salary,rownum as num from employees) where num between 2 and 5; //将rownum伪列变成物理列,再通过子查询查出来,
(3)、从中间查询方式,通过交集相减查询返回第2行到第5行
select employee_id,first_name,last_name,salary from employees where rownum<6 minus select employee_id,first_name,last_name,salary from employees where rownum<2;
2、不通过子查询或交集减返回中间的行数,直接通过fetch和offset,offset n是从头跳过n行,fetch first|next m是取前m行或接下来的m行,50 percent是取50%的数据,如果后面跟only,一半后的数据和前面相同也不会输出;如果后面跟with ties,那一半后的数据和前面相同的就会输出
select employee_id,first_name,last_name,salary from employees order by 1 fetch first 5 rows only; //取前5行,order by在fetch前面 select employee_id,first_name,last_name,salary from employees order by 1 offset 5 row fetch next 5 rows only; //跳过前面5行,取下面的5行 select employee_id,first_name,last_name,salary from employees order by 1 fetch first 50 percent rows only; select employee_id,first_name,last_name,salary from employees order by 1 fetch first 50 percent rows with ties;
详细知识讲解参考https://blog.csdn.net/mitedu/article/details/3584399
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下