当前位置:网站首页>Oracle中实现获取指定行内容——Rownum和Row_number()
Oracle中实现获取指定行内容——Rownum和Row_number()
2022-06-21 16:00:00 【牛奶咖啡13】
一、需求说明
在使用Oracle数据库进行业务开发的过程中需要获取到指定行的内容。
二、思路分析
经过查阅相关资料,发现可以使用Rownum和Row_number()实现我们想要的效果。
| 序号 | 名称 | 说明 |
| 1 | ROWNUM | 表示:用于从查询的结果集中返回对应行的编号,返回的第一行分配的是1,第二行是2,第三行是3等等,后面的依此类推,这个行伪字段可以用于限制查询返回的总行数(适用于分页、和指定行内容查询),而且rownum不能以任何表的名称作为前缀。 |
| 2 | Row_number() | ①row_number() over(partition by 需要分组的字段 order by 排序的字段 ); ②row_number() over(order by 排序的字段); 此函数计算的值就表示每组内部排序后的顺序编号(组内是连续唯一的) |
注意:【ROWNUM】是进行排序的时候是先对结果集加入虚拟列rownum然后再进行排序;而Row_number()则是在包含排序从句后是先排序再计算行编号。
三、实现方法
3.1、ROWNUM
3.1.1、查看内容对应的数据编号
--查看当前表格的内容与行号(语法)
SELECT ROWNUM,字段名 from 表名;

3.1.2、查看指定行内容
--查看指定行内容(语法)
SELECT t.* FROM (SELECT ROWNUM rm,字段名 from 表名) t WHERE rm=需要查看的行编号
3.1.3、对指定排序条件查看的内容分页
--对查询内容排序后分页(语法)
SELECT * FROM (SELECT ROWNUM rm,t.* FROM (SELECT 字段名 from 表名 ORDER BY 字段名) t ) WHERE rm>=开始编号 AND rm<=结束编号注意:如果需要对查询内容排序,就不能直接使用Rownum进行分页查询,因为编号顺序不对,如下图所示:

正确的对排序后的内容分页查询是使用子查询(即:先处理查询内容,然后在使用ROWNUM),如下图所示:


3.1.4、对指定分组排序条件查看的内容分页
----对指定分组排序条件查看的内容分页(语法)
SELECT * FROM (SELECT ROWNUM rm,t.* FROM (SELECT 字段名1,字段名2 from 表名 GROUP BY 字段名2,字段名1 ORDER BY 字段名2,字段名1) t ) WHERE rm>=开始行编号 AND rm<=结束行编号

3.2、Row_number()
3.2.1、查看内容对应的数据编号
--查询指定条件内容(语法)
SELECT row_number() over(order by 字段) rn,字段名 from 表名;

3.2.2、查看指定行你内容
--查看指定行内容(语法)
SELECT * FROM (SELECT row_number() over(order by 字段名) rn,字段名 from 表名) WHERE rn=需要查看的编号;

3.2.3、对指定排序条件查看的内容分页
SELECT * FROM (SELECT row_number() over(order by 字段名) rn,字段名 from 表名) WHERE rn>=开始行编号 AND rn<=结束行编号
3.2.4、对指定分组排序条件查看的内容分页
--对指定分组排序条件查看的内容分页
SELECT * FROM (SELECT row_number() over(partition by 需分组的字段名 order by 需排序的字段名 ) rn,字段名1,字段名2 from 表名) WHERE rn>=开始行的编号 AND rn<=结束行的编号

四、其他学习资料
Oracle中rownum和row_number()
https://www.codeleading.com/article/29563134126/oracle中row_number和rownum的区别和联系(翻译)
https://www.cnblogs.com/autumnlj/p/5815210.html
边栏推荐
- Advanced performance test series 5. server development, VMware virtual technology
- Circular of the State Council on regulating the management of the rental of housing with common property rights (for Trial Implementation)
- Advanced performance test series 4. premise, tool and process of performance test
- Implementation and landing of any to any real-time voice change RTC dev Meetup
- 接口自动化加解密
- Advanced performance test series 3. performance index, reliability test, capacity test and performance test
- qtcreator报错解决
- 互联网公司做单元测试吗?银行的需求有必要做单元测试吗?
- D improve translation
- KindEditor上传图片及使用
猜你喜欢

Pytest框架

之前的装机记录

IDC Consulting: in 2022, China's relational database software market is about to change

阿里云服务器+宝塔面板+无域名部署web项目

Unittest框架

使用unittest框架生成测试报告
![Undefined functions or variables [explained in one article] (matlab)](/img/fe/54272b8efce87ed7a78ac43b1fc189.png)
Undefined functions or variables [explained in one article] (matlab)

Google Play Academy 组队 PK 赛,正式开赛!

使用 Guzzle 中间件进行优雅的请求重试

学习软件“学习通”数据库疑似发生信息泄露,泄露学生信息达1亿多条
随机推荐
Google Earth engine (GEE) - sentinel-1 comprehensively check the difference between automatic landslide monitoring before and after two months (Guatemala as an example)
微信小程序-TabBar用法
Online text list batch add line number tool
AutoK3s v0.5.0 发布 延续简约和友好
Introduction to mqtt protocol
Disruptor local thread queue_ Workprocessor exception_ Fatalexceptionhandler--- inter thread communication work note 004
Generating test reports using the unittest framework
Standing at the digital tuyere, how can tooling enterprises "fly"
Web page automation practice "3. in elong, hotels are accurately matched according to city + date + keyword" part 2
Yaml文件详解
学习软件“学习通”数据库疑似发生信息泄露,泄露学生信息达1亿多条
Google Earth engine (GEE) - use sentinel-2 data acquisition to obtain the NDVI difference of one month ago (Guatemala as an example)
一个好产品应该具备的特征
阿里云服务器+宝塔面板+无域名部署web项目
【1108. IP 地址无效化】
Esp8266/esp32 get NTP time method through timelib Library
About SQL: does anyone in SQL know how to answer these questions?
Online shopping website (final assignment)
关于印发《北京市共有产权住房管理暂行办法》的通知
Huawei cloud releases desktop ide codearts