我用的是oracle 10g, 在一个表中我按一个字符型的非关键字段排序取前15条记录,发现一个问题。
该字段的内容有汉字,有字母。共20条记录。
用以下语句看到的顺序应该是正确的。
select * from ttransformer order by fctrfoname asc,fcTrfoCode asc
fctrfoname 为非关键字段,fcTrfoCode 为关键字段。
当我将语句变成
select * from ttransformer where rownum<=15 order by fctrfoname asc,fcTrfoCode asc
取前15条记录时发现,取出的15记录不是按语句 select * from ttransformer order by fctrfoname asc,fcTrfoCode asc
排序时看到的前15条,而是有点乱了。
这是什么问题?有解决的方法没有?
请高手指点!
该字段的内容有汉字,有字母。共20条记录。
用以下语句看到的顺序应该是正确的。
select * from ttransformer order by fctrfoname asc,fcTrfoCode asc
fctrfoname 为非关键字段,fcTrfoCode 为关键字段。
当我将语句变成
select * from ttransformer where rownum<=15 order by fctrfoname asc,fcTrfoCode asc
取前15条记录时发现,取出的15记录不是按语句 select * from ttransformer order by fctrfoname asc,fcTrfoCode asc
排序时看到的前15条,而是有点乱了。
这是什么问题?有解决的方法没有?
请高手指点!
select * from (select ROWNUM RN,A.* from ttransformer A order by fctrfoname asc,fcTrfoCode asc ) where rn<=15
select * from (select * from ttransformer order by fctrfoname asc,fcTrfoCode asc )
where rownum<=15;
这个语句是先从表中取出15条记录,然后对这15条进行排序.
要想取排序好之后的前15条,需要用yf520gn的方法.
select *
from (select * from ttransformer order by fctrfoname asc,fcTrfoCode asc)
where rownum <=15
select * from ttransformer where rownum <=15 order by fctrfoname asc,fcTrfoCode asc
如果排序字段是主键的话执行顺序是先排序再取rownum<=15如果是非主键,则是先取rownum <=15 再排序
所以如果想对非主键先排序再取rownum<=15只有通过1楼的方法实现