Oracle中查询rownum和rowid的区别
[url=http://database.51cto.com/]http://database.51cto.com[/url]
2009-05-25 10:00 ljh0242 51CTO博客 我要评论(
0
)
[list]
摘要:
在Oracle中,有一个很有趣的东西,那就是rownum。当你从某个表中查询数据的时候,返回的结果集中都会带有rownum这个字段,而且有时候也可以使用rownum进行一些条件查询。
标签:
[url=http://www.51cto.com/php/search.php?keyword=Oracle]Oracle[/url]
[url=http://www.51cto.com/php/search.php?keyword=rownum]rownum[/url]
[url=http://www.51cto.com/php/search.php?keyword=rowid]rowid[/url]
Oracle帮您准确洞察各个物流环节
[/list]
在查询中,我们可以注意到,类似于“select xx from table
where rownum 1)这样的查询是有正确含义的,而“select xx from table where
rownum = n”这样的查询只在n=1的时候成立,“select xx from table where rownum >
n”(n>1)这样的查询只能得到一个空集。另外“select xx from table where rownum >
0”这个查询会返回所有的记录。这是为什么呢?原因就在于Oracle对rownum的处理上,rownum是在得到结果集的时候产生的,用于标记结果集
中结果顺序的一个字段,这个字段被称为“伪数列”,也就是事实上不存在的一个数列。它的特点是按顺序标记,而且是逐次递加的,换句话说就是只有有
rownum=1的记录,才可能有rownum=2的记录。
让我们回头来分析一下在where中使用rownum作为查询条件的情况。在rownum取=1,或者rownum 1)的时候,没有问题。那么为什么当条件为rownum = n或者rownum >=
n时明明有数据却只能得到一个空集呢?假设我们的查询条件为rownum =
2,那么在查询出的第一条记录的时候,oracle标记此条记录rownum为1,结果发现和rownum=2的条件不符,于是结果集为空。写到这里,我
忽然有一个有趣的想法:假如有一条查询语句为select xx,yy from table where zz > 20 and
rownum 20的条件查询出一个结果集,然后按照rownum取出前10条返回?还是在按照zz>20的条件先
查询,然后有一个记录就标记一个rownum,到rownum20) where xyz between 10
and 20”这样就可以了。另外使用oracle提供的结果集处理函数minus也可以做到,例如“select xx,yy from table
where zz > 20 and rownum 20 and rownum 巧用dbms_rowid包获得rowid的详细信息
浅析Oracle中的表空间查询方法
Oracle数据库中的字符处理技巧
[url=http://developer.51cto.com/art/200902/110254.htm] [/url]
[/list]
【责任编辑:彭凡
TEL:(010)68476606】
[url=http://database.51cto.com/]http://database.51cto.com[/url]
2009-05-25 10:00 ljh0242 51CTO博客 我要评论(
0
)
[list]
摘要:
在Oracle中,有一个很有趣的东西,那就是rownum。当你从某个表中查询数据的时候,返回的结果集中都会带有rownum这个字段,而且有时候也可以使用rownum进行一些条件查询。
标签:
[url=http://www.51cto.com/php/search.php?keyword=Oracle]Oracle[/url]
[url=http://www.51cto.com/php/search.php?keyword=rownum]rownum[/url]
[url=http://www.51cto.com/php/search.php?keyword=rowid]rowid[/url]
Oracle帮您准确洞察各个物流环节
[/list]
在查询中,我们可以注意到,类似于“select xx from table
where rownum 1)这样的查询是有正确含义的,而“select xx from table where
rownum = n”这样的查询只在n=1的时候成立,“select xx from table where rownum >
n”(n>1)这样的查询只能得到一个空集。另外“select xx from table where rownum >
0”这个查询会返回所有的记录。这是为什么呢?原因就在于Oracle对rownum的处理上,rownum是在得到结果集的时候产生的,用于标记结果集
中结果顺序的一个字段,这个字段被称为“伪数列”,也就是事实上不存在的一个数列。它的特点是按顺序标记,而且是逐次递加的,换句话说就是只有有
rownum=1的记录,才可能有rownum=2的记录。
让我们回头来分析一下在where中使用rownum作为查询条件的情况。在rownum取=1,或者rownum 1)的时候,没有问题。那么为什么当条件为rownum = n或者rownum >=
n时明明有数据却只能得到一个空集呢?假设我们的查询条件为rownum =
2,那么在查询出的第一条记录的时候,oracle标记此条记录rownum为1,结果发现和rownum=2的条件不符,于是结果集为空。写到这里,我
忽然有一个有趣的想法:假如有一条查询语句为select xx,yy from table where zz > 20 and
rownum 20的条件查询出一个结果集,然后按照rownum取出前10条返回?还是在按照zz>20的条件先
查询,然后有一个记录就标记一个rownum,到rownum20) where xyz between 10
and 20”这样就可以了。另外使用oracle提供的结果集处理函数minus也可以做到,例如“select xx,yy from table
where zz > 20 and rownum 20 and rownum 巧用dbms_rowid包获得rowid的详细信息
浅析Oracle中的表空间查询方法
Oracle数据库中的字符处理技巧
[url=http://developer.51cto.com/art/200902/110254.htm] [/url]
[/list]
【责任编辑:彭凡
TEL:(010)68476606】