select * from (select * from tablename order by sys_guid()) where rownum < N; 
或者
select * from (select * from tablename order by dbms_random.value) where rownum< N; 
注:dbms_random包需要手工安装,位于$ORACLE_HOME/rdbms/admin/dbmsrand.sql 
dbms_random.value(100,200)可以产生100到200范围的随机数 对于你N=5

解决方案 »

  1.   

    1.查出记录条数据 select count(*) from A
    2.用程序生成5个在记录条数之内的5个随机数字:num1,num2,num3,num4,num5
    3.随机查询 select * from A where rownum in (num1,num2,num3,num4,num5)
      

  2.   

    我试了一下brucelau的方法,但不能随机查询,order by sys_guid()不能随机排序
      

  3.   

    从zyz2表中随机取出两条纪录:
    SQL> select * from zyz2;
           ID         P1         P2
    ---------- ---------- ----------
            1        211        212
            2         12         14
            4        241        242
            2        145
            1         11
            3         17          26 rows selected.Elapsed: 00:00:00.50
    SQL> select id,p1,p2 from (select zyz2.*,dbms_random.random num from zyz2 order
    by num) where rownum<3;       ID         P1         P2
    ---------- ---------- ----------
            2         12         14
            1        211        212Elapsed: 00:00:00.30
    SQL> select id,p1,p2 from (select zyz2.*,dbms_random.random num from zyz2 order
    by num) where rownum<3;       ID         P1         P2
    ---------- ---------- ----------
            3         17          2
            2         12         14
    8i以上
    select * from (select * from ur_table order by sys_guid()) where rownum<n;
    select * from (select * from ur_table order by dbms_random.value) where rownum<n;