前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >rownum浅析

rownum浅析

作者头像
小小明童鞋
发布2018-06-13 16:20:12
5790
发布2018-06-13 16:20:12
举报
文章被收录于专栏:java系列博客

先附上官网上的一段,然后是自己写的

ROWNUM

For each row returned by a query, theROWNUMpseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has aROWNUMof 1, the second has 2, and so on.

You can useROWNUMto limit the number of rows returned by a query, as in this example:

代码语言:javascript
复制
SELECT * FROM employees WHERE ROWNUM

<

10;

If anORDERBYclause followsROWNUMin the same query, then the rows will be reordered by theORDERBYclause. The results can vary depending on the way the rows are accessed. For example, if theORDERBYclause causes Oracle to use an index to access the data, then Oracle may retrieve the rows in a different order than without the index. Therefore, the following statement will not have the same effect as the preceding example:

代码语言:javascript
复制
SELECT * FROM employees WHERE ROWNUM

<

11 ORDER BY last_name;

If you embed theORDERBYclause in a subquery and place theROWNUMcondition in the top-level query, then you can force theROWNUMcondition to be applied after the ordering of the rows. For example, the following query returns the employees with the 10 smallest employee numbers. This is sometimes referred to astop-Nreporting:

代码语言:javascript
复制
SELECT * FROM

(SELECT * FROM employees ORDER BY employee_id)

WHERE ROWNUM

<

11;

In the preceding example, theROWNUMvalues are those of the top-levelSELECTstatement, so they are generated after the rows have already been ordered byemployee_idin the subquery.

Conditions testing forROWNUMvalues greater than a positive integer are always false. For example, this query returns no rows:

代码语言:javascript
复制
SELECT * FROM employees

WHERE ROWNUM

>

1;

The first row fetched is assigned aROWNUMof 1 and makes the condition false. The second row to be fetched is now the first row and is also assigned aROWNUMof 1 and makes the condition false. All rows subsequently fail to satisfy the condition, so no rows are returned.

You can also useROWNUMto assign unique values to each row of a table, as in this example:

代码语言:javascript
复制
UPDATE my_table

SET column1 = ROWNUM;

Please refer to the functionROW_NUMBERfor an alternative method of assigning unique numbers to rows.

查询前十个打卡的人

代码语言:javascript
复制
 select tt.*,rownum from (        
         select * from mydailydk dk         
         order by dk.dktime asc  ) tt
         where rownum <=10;

第是一个到第二十个打卡的人

代码语言:javascript
复制
 select * from (
   select tt.*,rownum rownum_ from (        
         select * from mydailydk dk         
         order by dk.dktime asc  ) tt
         where rownum <= 20)
         where rownum_ >10;    

上个功能用 row_num() 函数实现如下(主要用于 根据学科分组 取前几名 或者后几名等的时候用到)

代码语言:javascript
复制
 select * from (select id,currentday,name ,dktime,row_number() over (partition by name order by dktime asc) rownum_
 from mydailydk ) where rownum_ <=20 and rownum_ >10;
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档