使用Oracle10g的rownum时,我得到了一个令人困惑的结果。
select * from A where name like '%test' order by name asc
==>返回1条记录
select * from (
select * from A where name like '%test'
order by name asc
)
where rownum <= 2
==&GT;返回2条记录
如果我删除&#39;按顺序排列&#39;那么它将返回1条记录。
任何人都可以帮我解释一下这种行为吗?
答案 0 :(得分:0)
也许你想要这个功能:
select * from (
select X.*, rownum r from (
select * from A where name like '%test'
order by name asc
) X
)
where r <= 2
或者
select * from (
select A.*, ROW_NUMBER() OVER (ORDER BY name) r
from A where name like '%test'
)
where r <= 2