rownum的问题!

[复制链接]
查看11 | 回复7 | 2007-8-24 01:05:17 | 显示全部楼层 |阅读模式
(select rownum,j.a

from table1 j,table2 t

where j.l1='1111'

and rownum <8
)
minus
(select rownum,j.a

from table1 j,table2 t

where j.l1='1111'

and rownum <7
)
能够得到数据
7XXXX

(select j.a

from table1 j,table2 t

where j.l1='1111'

and rownum <8
)
minus
(select j.a

from table1 j,table2 t

where j.l1='1111'

and rownum <7
)
无法得到数据,请问是什么原因,还有我想得到
select j.a

from table1 j,table2 t

where j.l1='1111'
返回的最后一条数据,或某条ROWNUM固定的值,该怎样取得,
前提是不能是select * from(select rownum nu,j.a

from table1 j,table2 t

where j.l1='1111'

and rownum <7) where nu=7
因为其他语句限制,不能再嵌套了,请大家帮忙啊!
回复

使用道具 举报

千问 | 2007-8-24 01:05:17 | 显示全部楼层
你看看你得到的结果:7XXXX
里面的XXXX在
select j.a

from table1 j,table2 t

where j.l1='1111'

and rownum <8
是不是有重复,有的话一minus重复的都会不见,但如果你用了
select rownum,j.a

from table1 j,table2 t

where j.l1='1111'

and rownum <8
就能保证每一行都是唯一的,所以8行减7行就得到了你的结果:7XXXX
回复

使用道具 举报

千问 | 2007-8-24 01:05:17 | 显示全部楼层
rownum是会变化的,并不是如你想像的那样,不同的查询条件rownum都可能不一样
回复

使用道具 举报

千问 | 2007-8-24 01:05:17 | 显示全部楼层
minus的问题:在minus之前两个集合会作distinct, 如果你第一个集合里有重复的记录,减完了就变成空集。带ROWNUM保证每行都不一样,DISTINCT 完还是7条和6条。
第二个问题:因为你有rownum <7,所以RU=7是永远不成立的。改为rownum <=7,虽然可以但不知道有什么意义,因为未经排序的ROWNUM基本上可以理解为随机数。
回复

使用道具 举报

千问 | 2007-8-24 01:05:17 | 显示全部楼层
谢谢大家的解答!
第二个问题是这样的
select * from(select rownum nu,j.a

from table1 j,table2 t

where j.l1='1111'

) where nu=6
我想只一个像
select rownum nu,j.a

from table1 j,table2 t

where j.l1='1111' AND NU=6
语句得到结果集的某一行,看了一下其他的资料,都是要再嵌套一次,可不可以一次查询得到呢?
回复

使用道具 举报

千问 | 2007-8-24 01:05:17 | 显示全部楼层
先去了解一下rownum的原理吧,不然和你说了下次你还是不知道!
回复

使用道具 举报

千问 | 2007-8-24 01:05:17 | 显示全部楼层
原帖由 zeees 于 2008-9-25 09:25 发表
谢谢大家的解答!
第二个问题是这样的
select * from(select rownum nu,j.a

from table1 j,table2 t

where j.l1='1111'

) where nu=6
我想只一个像
select rownum nu,j.a

from table1 j,table2 t

where j.l1='1111' AND NU=6
语句得到结果集的某一行,看了一下其他的资料,都是要再嵌套一次,可不可以一次查询得到呢?

第二個肯定沒結果
回复

使用道具 举报

千问 | 2007-8-24 01:05:17 | 显示全部楼层
给rownum 起个别名,因为它是伪列哦
以下是它的原理:
1 Oracle executes your query.
2 Oracle fetches the first row and calls it row number 1.
3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.
4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).
5 Go to step 3.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行