Sample恐怕是不行的,试一下这2个:
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;

解决方案 »

  1.   

    select * from yourtable order by newid()
      

  2.   

    关于Sample,我也不太确定;按照实际的尝试,他连续几次取出重复的记录机会比较大;而且也不能保证取出的记录是n条
      

  3.   

    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;
    这两个看起来可以,但是能解释一下吗?
    select * from yourtable order by newid()
    这个不行,newid()是什么东西? 能说明一下吗?
      

  4.   

    兄弟:
    这个问题你应该去Oracle板块提问啊1、使用dbms_random程序包, 取出随机数据。先创建序列
    create sequence tmp_id increment by 1 start with 1 maxvalue 9999999 nocycle nocache;
    2. 然后创建一个临时表tmp_1,记录全部取出来。
    create table tmp_1 as select tmp_id.nextval as id,email,mobileno from 表名 where 条件;
      找到最大的id号:   
      select max(id) from tmp_1;
    3、设定一个生成随机数的种子
      execute dbms_random.seed(12345678);
    4、调用随机数生成函数dbms_random.value生成临时表tmp_2,假设随机取200个
    create table tmp_2 as select trunc(dbms_random.value(1,5000)) as id from tmp_1 where rownum<201;
     
    说明:dbms_random.value(1,5000)是取1到5000间的随机数,会有小数,
          trunc函数对随机数字取整,才能和临时表的整数ID字段相对应。
      

  5.   

    SELECT *
    FROM Northwind..Orders 
    ORDER BY NEWID()
      

  6.   

    SELECT RAND( (DATEPART(mm, GETDATE()) * 100000 )
               + (DATEPART(ss, GETDATE()) * 1000 )
               + DATEPART(ms, GETDATE()) )
      

  7.   

    我晕呀,这么多星星的方法都这么笨!!Select top 1 * from table where Id>newId
      

  8.   

    SQLSERVER
    select top n * from table order by newid()
      

  9.   

    NewId
    的值随你怎么得到,只要小于你的Id最大值就行!!Select top 1 * from table where Id>newId
    //随机数的函数我想不起来!!
      

  10.   

    如果要链接子表可用(有重复记录)
    select top n a.field from table a left join  table b on a.id = b.id 
    group by a.field order by newid()
      

  11.   

    参考--http://authors.aspalliance.com/stevesmith/articles/randomselect.asp