select * from <table> where rownum < 11select * from (select * from <table> order by <col> desc) where rownum < 11

解决方案 »

  1.   

    select * from tablname where rownum<=10
      

  2.   

    select * from tabname where rownum<=10
      

  3.   

    再没有好的方法了吗?
    就像MS_SQL那样只用一个关键字TOP就可以查出前几条记录了!!!
      

  4.   

    oracle 中没有更好的方法了。
    rownum 是唯一的最简单的方法了。
      

  5.   


    ROWNUM returns a number indicating the order in which a row was selected from a table. The first row selected has a ROWNUM of 1, the second row has a ROWNUM of 2, and so on. If a SELECT statement includes an ORDER BY clause, ROWNUMs are assigned to the retrieved rows before the sort is done. You can use ROWNUM in an UPDATE statement to assign unique values to each row in a table. Also, you can use ROWNUM in the WHERE clause of a SELECT statement to limit the number of rows retrieved, as follows: DECLARE
       CURSOR c1 IS SELECT empno, sal FROM emp
          WHERE sal > 2000 AND ROWNUM < 10;  -- returns 10 rows
    The value of ROWNUM increases only when a row is retrieved, so the only meaningful uses of ROWNUM in a WHERE clause are ... WHERE ROWNUM < constant;
    ... WHERE ROWNUM <= constant;
      

  6.   

    ROWNUM 和 ROWID同属于伪列,
    满足你所提的需求典型应用是:先排序,后取出前几条记录,如果只用ROWNUM而不排序的话,取出来的并不是你想要的数据,
    不信你可试试,每次结果都会不同
      

  7.   

    select * from yourtable where rownum<11;
    这是ORACLE中最简单的方法了!
      

  8.   

    (select * from yourtable where rownum<11)
    minus
    (select * from yourtable where rownum<21)
    但是好象不支持排序。
      

  9.   

    8i以上可以用这个排序
    select * from 
    (select rowid id,t.* from table t order by field)
    where id between 10 and 20
      

  10.   

    (select * from yourtable where rownum<21)
    minus
    (select * from yourtable where rownum<11)
    好像是这样吧
      

  11.   

    select * from 
    (select rownum rm,t.* from table t order by field)
    where rm between 10 and 20这种方法不行,属于先分后排
      

  12.   

    select * from 
    (select * from table t order by field)
    where rm < 11
      

  13.   

    select * from (select * from tbname order by rolname) where rownum<11;
      

  14.   

    你可以在你的表中加入,数据添加字段。然后再用rownum伪列筛选你的所需。
      

  15.   

    数据量很大的话,岂不是和没加rownum更慢?
      

  16.   

    我做了一个试验,ora8i,发现commit(滩涂鱼)说得并不正确,没有加order by子句,也是按照顺序取数据。
    还有如果按照commit(滩涂鱼)的说法:先排序,后取数据的话,应该是很慢的(测试数据由65536条,一条select语句大概需要45秒左右再developer中),但是事实上速度很快,取出前10条仅用了0.047秒
      

  17.   

    select * from tablname where rownum<=10
    这还不简单吗
      

  18.   

    如果只是简单的rownum<=10的话,结果并不是想要的结果。不信可以试试看。
    争取的应该是:select * from (select * from a order by id) a where rownum<3。
    看下面的实验:
    SQL> select * from a;DATEA            ID DATEB
    --------- --------- ---------
    12-MAR-03         2 01-FEB-01
    01-FEB-01         4 01-FEB-01
    16-MAR-03         1 16-MAR-03
    02-FEB-03         3 16-MAR-03SQL> select * from a where rownum<3;DATEA            ID DATEB
    --------- --------- ---------
    12-MAR-03         2 01-FEB-01
    01-FEB-01         4 01-FEB-01SQL> select * from a where rownum<3;DATEA            ID DATEB
    --------- --------- ---------
    12-MAR-03         2 01-FEB-01
    01-FEB-01         4 01-FEB-01很明显上面的结果并不是想要的结果。如果想要按照某一个条件排序的话,只能按照下面的方法:
    SQL> select * from (select * from a order by id) a where rownum<3DATEA            ID DATEB
    --------- --------- ---------
    16-MAR-03         1 16-MAR-03
    12-MAR-03         2 01-FEB-01
    这才是想要的结果。
    但是比较麻烦。我也想知道有没有简单的方法。
    关注!!!
    up!!!!!up again!!!
      

  19.   

    select * from (select * from a order by id) a where rownum<10;
    没有比它更简单的了吗?
      

  20.   

    想查找中间一段的结果集,可如下写法:
    select * from (select rownum id, fieldname1, fieldname2 from tablename) where 
    id > 7 and id < 10
      

  21.   

    试试看!
    select * from (select * from tablename order by fieldname1) where rownum<=10;
      

  22.   

    最近刚好也在想这个问题,把楼上的意见看了一遍,有些想法,写出来交流一下,先声明我现在用的是Oracle 9.0.1.0.0,这个也许很重要!!    supershb(phenix) ,Strawberry79(草莓) 意思似乎是,像select * from table_name where rownum < n order by col 这样的句子,并不能达到先排序后检索地目的,但在我这里是可以的!!    penitent(只取一瓢) 所说的,在9i 中好像不行,rowid不是简单的数字!      select * from (select * from table_name order by col) where rownum < n 达到目的,但如果select * from table_name where rownum < n order by col 可以的话,还是这样好!    查找中间段,johncs(阿吉) 是对的!!    laofei2000() 所说的好像有点问题,“有加order by子句,也是按照顺序取数据”,按哪个字段呀,总不会想要哪个就是哪个!!
      

  23.   

    更正一下,order by 确实在后面,顺序是这样,先按rowid取n条记录,然后按order by, 刚才有误!对索引列好像是可以先排序的!!
      

  24.   

    ft,为什么这么多人都能用
    select * from (select * from <table> order by <col> desc) where rownum < 11
    这个句式,我就死活不行.老是报错说缺少右括号.去掉ORDER BY子句就可以通过.
    我用的是8.0.5 FOR NT,难道是因为版本太老?
      

  25.   

    SELECT A FROM TABLE WHERE ROWNUM<11 ORDER BY A;