小弟新手,请教各位大侠:在oracle中如何查询一条随机记录?小弟不胜感激!
我在网络中查了一下,好像要采用random(),我写了:select * from  table_name  order by random() limit 1;也来回改了好几次,但是总得不到答案,显示:sql命令未正确结束,请教各位高手解决一下!谢谢了!

解决方案 »

  1.   


    select *
    from (
          select * 
          from table_name
          order by dbms_random.value)
    where rownum=1
      

  2.   


    --在oracle里面,分页请使用rownum,oracle不支持mysql的limit分页
    SQL> select *
      2  from (
      3        select owner,object_name,created,status
      4        from all_objects
      5        order by dbms_random.value)
      6  where rownum=1
      7  /
     
    OWNER                          OBJECT_NAME                    CREATED     STATUS
    ------------------------------ ------------------------------ ----------- -------
    DMSYS                          DM_CL_BUILD                    2004-03-10  VALID
     
    SQL> /
     
    OWNER                          OBJECT_NAME                    CREATED     STATUS
    ------------------------------ ------------------------------ ----------- -------
    SYS                            java/beans/MethodDescriptor    2004-03-10  VALID
     
    SQL> /
     
    OWNER                          OBJECT_NAME                    CREATED     STATUS
    ------------------------------ ------------------------------ ----------- -------
    SYS                            /a4b44102_JavacErrorsText_zh_T 2004-03-10  VALID
     
    SQL> /
     
    OWNER                          OBJECT_NAME                    CREATED     STATUS
    ------------------------------ ------------------------------ ----------- -------
    PUBLIC                         /a5244b44_JobImpressionsComple 2004-03-10  VALID
     
    SQL> /
     
    OWNER                          OBJECT_NAME                    CREATED     STATUS
    ------------------------------ ------------------------------ ----------- -------
    PUBLIC                         sqlj/util/BlockDescriptor      2004-03-10  VALID
    oracle 通过rownum实现分页 
      

  3.   

    上面的想法是好的,但却无法完全满足随机这个概念,同时,还存在小问题。问题:
             表字段数量 < dbms_random.value , 是否会报错,导致无法执行? 
                  建议,考虑指定随机数的范围。疑问:
              按上面的解法,只是随机按某个字段排序,有一定随机性,但随机范围仍然受到限制,并未达到完全随机的要求。
                  目前所能想到的解法,按行号进行随机抽取。
                     1. 所有数据行手工给定行号
                     2. 随机抽取一行号(通过随机函数)
                     但缺点也是明显的,就是性能。 如果表中数据较多时,需要花费很长时间。  所以,等待高人给出一个更为合理的解决方案。
      

  4.   

    --从一个表中随机取一条记录
    --1.dbms_random.value
    select * from(select * from table1 order by dbms_random.value) where rownum < 2;
    --2.dbms_random.random
    select * from(select * from table1 order by dbms_random.random) where rownum < 2;
    --3.sys_guid()
    select * from(select * from table1 order by sys_guid()) where rownum < 2;
    --4.sample(20)       按百分比
    select * from(select * from table1 sample(20)) where rownum < 2;
    --5.sample block(20) 按数据块
    select * from(select * from table1 sample block(40)) where rownum < 2;
      

  5.   


    --确实,随机数应该有个范围,下面我们来找出 <=50的随机数,这里去5个
    select r
    from (
          select r
          from (
                select rownum r
                from all_objects
                where rownum <= 50)
          order by dbms_random.value)
    where rownum <= 5
    /
             R
    ----------
            36
            14
            41
            38
            46
     
    SQL> /
     
             R
    ----------
            34
             3
            42
            45
            37
     
    SQL> /
     
             R
    ----------
             4
            36
             5
             9
            49
     
    SQL> /
     
             R
    ----------
            31
            15
            29
            26
            39
     
    SQL> /
     
             R
    ----------
            20
            14
            29
            43
            48或者使用piplelined table function来做,参考:

     Oracle Pipelined Table Functions简介
      

  6.   


    SQL> select *
      2  from (
      3        select *
      4        from (
      5              select level
      6              from dual
      7              connect by level <=50 )
      8        order by dbms_random.random)
      9  where rownum <= 5
     10  /
     
         LEVEL
    ----------
             3
            26
             4
            34
            11
     
    SQL> /
     
         LEVEL
    ----------
            39
            28
            44
            38
            24
     
    SQL> /
     
         LEVEL
    ----------
             4
            50
            25
            49
             9
     
    SQL> /
     
         LEVEL
    ----------
            10
            12
            17
             8
            29
     
    SQL> /
     
         LEVEL
    ----------
            40
             7
            20
            31
             3