你试试下面的SQL语句:
SELECT TOP 10 * FROM tableName ORDER BY columnName

解决方案 »

  1.   

    错,TOP是T-SQL的关键字,PL/SQL里没有。这样:
    select * from table WHERE ROWNUM<=10 order by columnname 
                        ~~~~~~~~~~~~~~~~ROWNUM是ORACLE自动生成的伪列
      

  2.   

    可是倒序排列怎么办?有desc的话rownum就没用了。
    关注!!!
      

  3.   

    给冰火岛:我在ora73下怎的执行不了你的语句
    SELECT TOP 10 * FROM tableName ORDER BY columnName 
      

  4.   

    好象Oracle的执行顺序是先Where再Order,
    所以这个要求是不行了.
    只好先全部取出,排序,再取前10.
      

  5.   

    如果先建 view create view viewname as select * from table order by columnname desc
    然后 select * from viewname where rownum<=10可以吗?
    我没有试过
      

  6.   

    同意yiwei(垃圾) 
    如果倒序 DESC 就可以了
      

  7.   

    用ROWNUM就不能用ORDER BY!不信你们可以查有关资料,用ORDER BY 也不报错,但取出来的数据是未经过排序的!
      

  8.   

    不是吧。order by应该没问题,order by desc才没用。
    这个问题似乎是oralce的sql的死角,用一条语句应该是实现不了的。
    继续强烈关注!!!
      

  9.   

    select * from (select * from tablename order by columnname desc) where rownum<=10
      

  10.   

    haihong():
       这句你试了吗?通不过的,括号里还是不能有order by。另外:这样的语句有意义吗?我的意思是效率。
      

  11.   

    Oracle的文档是这么说的:Oracle assigns a ROWNUM value to each row as it is retrieved, before rows are sorted for an ORDER BY clause, so an ORDER BY clause normally does not affect the ROWNUM of each row. However, if an ORDER BY clause causes Oracle to use an index to access the data, Oracle may retrieve the rows in a different order than without the index, so the ROWNUMs may be different than they would be without the ORDER BY clause. ???
      

  12.   

    create table ttt(t1 int)
    insert some values to ttt;
    select t1 from ( select t1 from ttt group by t1 ) where rownum < 5以为group by 会自动排序,希望对你有用。
      

  13.   

    to: haihong() 
    倒序!!!倒序怎么办???还有效率问题,你的语句岂不是没事找事,还不如全取出来然后只读前5条呢。
      

  14.   

    倒序也好办呀
    select -1*t1 from ( select -1*t1 from ttt group by t1 ) where rownum < 5
    呵呵
      

  15.   


    to hcduguo: http://www.csdn.net/expert/Topic/80398.shtm 也是关于这个问题的吧。
    Oracle的rownum 与 order by 是有问题?? 
    如果确实不行的话,还是建立一个排序后的标示字段,根据这个标识字段的取值范围来确定读取记录。
      

  16.   

    haihong的方法是好像是解决oracle这类问题最好的方法!
    select * from (select * from T order by col desc) where rownum <= 5这句我在oracle8.1.6好像没有问题!woodpan:
       倒想请教,什么是前5条纪录??? 不排序,用什么方法来找出前五条纪录!如果不把整张表(或是满足where条件的纪录)排序,数据库该怎样返回你‘前’5条纪录!
       
      

  17.   

    我觉得如果要提高效率的话,可以试试这样做
    首先估计个大概的值est_value,也就是前几个大概的大小
    然后
    select * from (select * from t where col1>est_value) where rownum<=5 order by col1;
    先选择出比est_value大的记录,然后再排序,这样子比全部排序的快。
    如果没办法作出个大体的估计,估计快不起来
      

  18.   

    同意tangtao的问题,SQL执行返回的记录集是一个集合的概念,个个元素之间是没有先后顺序的
      

  19.   

    我的方法有什么问题么?我试过,很正确啊。SQL*Plus: Release 8.0.4.0.0 - Production on 星期五 3月 9 15:52:0 2001(c) Copyright 1997 Oracle Corporation.  All rights reserved.
    连接到:
    Oracle8i Enterprise Edition Release 8.1.6.1.0 - Production
    With the Partitioning option
    JServer Release 8.1.6.0.0 - ProductionSQL> select logid,starttime,rownum from calllog where rownum<=20 order by starttime desc;    LOGID STARTTIME     ROWNUM
    --------- ---------- ---------
        71100 01-1月 -01        20
        71099 01-1月 -01        19
        71098 01-1月 -01        18
        71097 01-1月 -01        17
        71096 01-1月 -01        16
        71095 01-1月 -01        15
        71094 01-1月 -01        14
        71093 01-1月 -01        13
        71092 01-1月 -01        12
        71091 01-1月 -01        11
        71090 01-1月 -01        10
        71089 01-1月 -01         9
        71088 01-1月 -01         8
        71087 01-1月 -01         7
        71086 01-1月 -01         6
        71085 01-1月 -01         5
        71084 01-1月 -01         4
        71083 01-1月 -01         3
        71082 01-1月 -01         2
        71081 01-1月 -01         1查询到20记录.SQL> calllog表以logid为PRIMARY KEY,在starttime上有索引
      

  20.   

    logid为使用触发器实现自动递增的标识字段
      

  21.   

    TO yiwei
     你的logid本来就是按数据产生的先后顺序产生的,
     你换一个不是主键的列排序看看
      

  22.   

    好啦好啦,诸位,这个问题早就该结了。liguangyi贴的那段e文已经说的很明白了。haihong的想法倒是很有创意,可惜没什么实用价值。
    to:tangtao
    我的意思是不限制取出条数,排序就随你怎么排了哇。
      

  23.   

    OK,叫我试,我就试,事实就是事实,说什么都没有用。SQL*Plus: Release 8.0.4.0.0 - Production on 星期五 3月 9 20:45:57 2001(c) Copyright 1997 Oracle Corporation.  All rights reserved.
    连接到:
    Oracle8i Enterprise Edition Release 8.1.6.1.0 - Production
    With the Partitioning option
    JServer Release 8.1.6.0.0 - ProductionSQL> select rownum,logid,starttime,callingnumber from calllog where rownum<=20 order by callingnumbe
    r desc;   ROWNUM     LOGID STARTTIME  CALLINGNUMBER
    --------- --------- ---------- -------------------------
           10     71090 01-1月 -01 098888888
            9     71089 01-1月 -01 088888888
            8     71088 01-1月 -01 078888888
            7     71087 01-1月 -01 068888888
            6     71086 01-1月 -01 058888888
            5     71085 01-1月 -01 048888888
            4     71084 01-1月 -01 038888888
            3     71083 01-1月 -01 028888888
           20     71100 01-1月 -01 0198888888
           19     71099 01-1月 -01 0188888888
            2     71082 01-1月 -01 018888888
           18     71098 01-1月 -01 0178888888
           17     71097 01-1月 -01 0168888888
           16     71096 01-1月 -01 0158888888
           15     71095 01-1月 -01 0148888888
           14     71094 01-1月 -01 0138888888
           13     71093 01-1月 -01 0128888888
           12     71092 01-1月 -01 0118888888
           11     71091 01-1月 -01 0108888888
            1     71081 01-1月 -01 008888888查询到20记录.SQL> 
      

  24.   

    SQL> select rownum,logid,starttime,callingnumber,uselineno from calllog where rownum<=10 order by us
    elineno desc;   ROWNUM     LOGID STARTTIME  CALLINGNUMBER             USELINENO
    --------- --------- ---------- ------------------------- ---------
           10     71090 01-1月 -01 098888888                         9
            9     71089 01-1月 -01 088888888                         8
            8     71088 01-1月 -01 078888888                         7
            7     71093 01-1月 -01 068888888                         6
            6     71086 01-1月 -01 058888888                         5
            5     71085 01-1月 -01 048888888                         4
            4     71087 01-1月 -01 038888888                         3
            3     71083 01-1月 -01 028888888                         2
            2     71082 01-1月 -01 018888888                         1
            1     71081 01-1月 -01 008888888                         0查询到10记录.SQL> 
    更特殊的,这里areacode没有索引。我还能说什么?事实摆在眼前
      

  25.   

    不是,是USELINENO没有索引
    SQL> select rownum,logid,starttime,callednumber,uselineno from calllog where rownum<=10 order by cal
    lednumber desc;   ROWNUM     LOGID STARTTIME  CALLEDNUMBER              USELINENO
    --------- --------- ---------- ------------------------- ---------
            7     71087 01-1月 -01 951289                            6
            6     71086 01-1月 -01 951288                            5
            5     71085 01-1月 -01 951287                            4
            4     71084 01-1月 -01 951286                            3
            3     71083 01-1月 -01 951285                            2
            2     71082 01-1月 -01 951284                            1
            1     71081 01-1月 -01 951283                            0
           10     71090 01-1月 -01 9512812                           9
            9     71089 01-1月 -01 9512811                           8
            8     71088 01-1月 -01 9512810                           7查询到10记录.SQL> 这个也没有索引
      

  26.   

    Oracle不保证能用,就算你试出来,又有人敢用到关键应用,如股票交易这样的系统上吗?
    别争了.
      

  27.   

    select * from table WHERE ROWNUM<=10 order by columnname 
                        
      

  28.   

    嘿嘿,仔细看了看那段E文,ORACLE没有说不能用。大家仔细看看吧,那段文字的大意是这样的:
    “Oracle 分配一个ROWNUM给每一行被检索出来的数据,在行被一个ORDER BY 子句排序之前,因此一个ORDER BY 子句通常不会影响每一行的ROWNUM。然而,如果一个ORDER BY 子句促使Oracle使用一个索引去访问数据,Oracle可能用一种不同于没有索引的顺序来检索那些行,因此那些ROWNUM可能不同于没有使用ORDER BY 子句时的情况。”
    真是一点都没有错,看看我的那些试验结果吧。:)
      

  29.   

    select * from tablename where rownum<=10 order by columnname;
    select * from tablename where rownum<=10 order by columnname desc;在8.0里都可以实现,难道在8.1里不可吗?我在ORACLE过程中,只发现了,在8.1中能用,但在8.0中不能,却没有发现过在8.0中能用,在8.1中不能用的句。
      

  30.   


    To yiwei(垃圾):
    你做试验用的记录集,选得实在是不合适,随便选就成么?而且不听... 这样做程序,才是 -- 
    I 服 了 U 
    看看下面的记录:oracle8.0.5、Win2000 server。
    AA列有索引, BB、CC列 没有索引。SQL> desc test;
     Name                            Null?    Type
     ------------------------------- -------- ----
     AA                                       VARCHAR2(10)
     BB                                       VARCHAR2(10)
     CC                                       NUMBERSQL> select * from test;AA         BB                CC
    ---------- ---------- ---------
    12         01                 1
    13         02                 1
    1201       03                 0
    14         04                 0
    1302       05                 1
    101        022               -1
    121        02                 0
    142        03                 18 rows selected.SQL> select rownum, aa,bb,cc from test where rownum <=5 order by aa;   ROWNUM AA         BB                CC
    --------- ---------- ---------- ---------
            1 12         01                 1
            3 1201       03                 0
            2 13         02                 1
            5 1302       05                 1
            4 14         04                 0SQL> select rownum, aa,bb,cc from test where rownum <=5 order by aa desc;   ROWNUM AA         BB                CC
    --------- ---------- ---------- ---------
            4 14         04                 0
            5 1302       05                 1
            2 13         02                 1
            3 1201       03                 0
            1 12         01                 1SQL> select rownum, aa,bb,cc from test where rownum <=5 order by bb;   ROWNUM AA         BB                CC
    --------- ---------- ---------- ---------
            1 12         01                 1
            2 13         02                 1
            3 1201       03                 0
            4 14         04                 0
            5 1302       05                 1SQL> select rownum, aa,bb,cc from test where rownum <=5 order by bb desc;   ROWNUM AA         BB                CC
    --------- ---------- ---------- ---------
            5 1302       05                 1
            4 14         04                 0
            3 1201       03                 0
            2 13         02                 1
            1 12         01                 1SQL> select  aa,bb,cc from test where rownum <=5 order by bb desc;AA         BB                CC
    ---------- ---------- ---------
    1302       05                 1
    14         04                 0
    1201       03                 0
    13         02                 1
    12         01                 1结果很显然,rownum比order by 出现的早,大家还是想别的办法吧。
    结果很显然,rownum比order by 出现的早,大家还是想别的办法吧。 
      

  31.   

    rownum 好像是按物理序排列的,有order by 时是先生成rownum的
    可以建临时表完成
    只查询欲查询表中的主键,建立temporary表,然后再用rownum取出前十条
    这样效率不会很低的
      

  32.   

    select -1*t1 from ( select -1*t1 from ttt group by t1 ) where rownum < 5
      ”-1*t1“ 怎么用啊?
      

  33.   

    To yiwei(垃圾)等:
    select rownum, aa,bb,cc from test where rownum <=5 order by aa;
    这种是把前5条排序
    人家是要所有的排序,再取前5条
      

  34.   

    你可以这样做嘛,先排序,再取前5条
    select * from (select * from abcd order by a) where rownum<=5;
      

  35.   

    大家的思路好像都集中在ORDERBY 和ROWNUM
      

  36.   

    大家的思路好像都集中在ORDERBY 和ROWNUM上,如果你开发过ORACLE的FORM,就会很快得到答案。使用游标或者称为事务,可很好解决这个问题:
    declare 
      i integer;
     rec record like.表....(不确,查相关资料)
      cursor cur_dd is
         select * FROM 表 ORDER BY 列 DESC 
    Begin
        i:=0;
      open cur_dd;
      loop
      fetch cur_dd into rec
       i:=i+1;
     exit when i>10
     显示该条记录
    end loop;