关于随机抽取的意思是,每执行一次这个SQL语句,每一组中抽到的一名老师不是同一个人。---------------------------------------------------------------------------------ID号 姓名 学校 65 苏国清 南京市合川小学 
406 刘昊斌 南京市合川小学 
427 高峰 南京市合川小学 444 庞春云 南京市广盛小学 
478 方富贵 南京市广盛小学 
480 刘铭 南京市广盛小学 
565 杨林 南京市广盛小学 655 张红艳 南京市汉衡小学 
921 邹忠臣 南京市汉衡小学 935 吴运新 南京市国联小学 
965 赵刚 南京市国联小学 
999 王志宏 南京市国联小学 
1099 王军 南京市国联小学 
2559 王宇 南京市国联小学 290608 丛志波 南京市方正小学 293546 朱东兴 南京市合川小学 

解决方案 »

  1.   

    可以一次抽取多个啊
    SELECT TOP N FROM TABLE ORDER BY NEWID()
      

  2.   

    select cast(RAND() * 10000 as Integer)取随机数
      

  3.   


    我想执行一次SQL语句随机抽取一个,每次抽取的一个都不相同
      

  4.   

    --> Title:生成測試數據
    -->Author:wufeng4552【水族杰纶】
    -->Date :2009-08-18 08:42:04
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([ID号] int,[姓名] nvarchar(3),[学校] nvarchar(7))
    Insert tb
    select 65,N'苏国清',N'南京市合川小学' union all
    select 406,N'刘昊斌',N'南京市合川小学' union all
    select 427,N'高峰',N'南京市合川小学' union all
    select 444,N'庞春云',N'南京市广盛小学' union all
    select 478,N'方富贵',N'南京市广盛小学' union all
    select 480,N'刘铭',N'南京市广盛小学' union all
    select 565,N'杨林',N'南京市广盛小学' union all
    select 655,N'张红艳',N'南京市汉衡小学' union all
    select 921,N'邹忠臣',N'南京市汉衡小学' union all
    select 935,N'吴运新',N'南京市国联小学' union all
    select 965,N'赵刚',N'南京市国联小学' union all
    select 999,N'王志宏',N'南京市国联小学' union all
    select 1099,N'王军',N'南京市国联小学' union all
    select 2559,N'王宇',N'南京市国联小学' union all
    select 290608,N'丛志波',N'南京市方正小学' union all
    select 293546,N'朱东兴',N'南京市合川小学'
    Go
    select * from tb t where [ID号]=(select top 1 [ID号] from tb where [学校]=t.[学校] order by newid())
    /*
    ID号         姓名   学校
    ----------- ---- -------
    65          苏国清  南京市合川小学
    565         杨林   南京市广盛小学
    655         张红艳  南京市汉衡小学
    1099        王军   南京市国联小学
    290608      丛志波  南京市方正小学(5 個資料列受到影響)
    */
      

  5.   


    select top 1 from table order by newid()
      

  6.   

    SELECT TOP 1 * FROM TB T 
    WHERE NOT EXISTS(SELECT 1 FROM TB WHERE 学校=T.学校  AND ID>T.ID ORDER BY NEWID())
      

  7.   

    order by newid()不能保证每次取的都不一样,等高人
      

  8.   

    --借用一下水哥的数据
    --此方法只适合于2000以上的版本
    if not object_id('tb') is null
        drop table tb
    Go
    Create table tb([ID号] int,[姓名] nvarchar(3),[学校] nvarchar(7))
    Insert tb
    select 65,N'苏国清',N'南京市合川小学' union all
    select 406,N'刘昊斌',N'南京市合川小学' union all
    select 427,N'高峰',N'南京市合川小学' union all
    select 444,N'庞春云',N'南京市广盛小学' union all
    select 478,N'方富贵',N'南京市广盛小学' union all
    select 480,N'刘铭',N'南京市广盛小学' union all
    select 565,N'杨林',N'南京市广盛小学' union all
    select 655,N'张红艳',N'南京市汉衡小学' union all
    select 921,N'邹忠臣',N'南京市汉衡小学' union all
    select 935,N'吴运新',N'南京市国联小学' union all
    select 965,N'赵刚',N'南京市国联小学' union all
    select 999,N'王志宏',N'南京市国联小学' union all
    select 1099,N'王军',N'南京市国联小学' union all
    select 2559,N'王宇',N'南京市国联小学' union all
    select 290608,N'丛志波',N'南京市方正小学' union all
    select 293546,N'朱东兴',N'南京市合川小学'
    GoSELECT [ID号],[姓名],[学校]
    FROM
    (
       SELECT rid=ROW_NUMBER() OVER(PARTITION BY [学校] ORDER BY NEWID() ),*
       FROM tb
    ) AS T
    WHERE rid<=1
    /*ID号         姓名   学校
    ----------- ---- -------
    290608      丛志波  南京市方正小学
    478         方富贵  南京市广盛小学
    935         吴运新  南京市国联小学
    921         邹忠臣  南京市汉衡小学
    427         高峰   南京市合川小学(5 行受影响)
    */
      

  9.   

    个别次有重复也可以,并不绝对,但大多数次运行SQL语句只要不同,就感谢了
      

  10.   

    数据量大的话 用newid()会有效率问题!
    用随机数rand()比较好
      

  11.   

    关键是我用的是access数据库,好像不支持newid()这个函数,用什么代替
      

  12.   

    select top(1) * from t order by newid();
      

  13.   


    rand()在SQL语句中的具体用法,结合我给出的实例写一下好吗
      

  14.   

    本来top 1是取一组中的一条记录,但用
    select * from tb t where [ID号]=(select top 1 [ID号] from tb where [学校]=t.[学校] order by newid())
    有时取到的是3条或更多条记录,是加order by newid()的缘故
    如何解决啊
      

  15.   

    这是因为取了ID号的原因,因为ID号相同的有多个,所以取出来的就多个了,前面加个TOP 1 就好了
    select TOP 1 * from tb t where [ID号]=(select top 1 [ID号] from tb where [学校]=t.[学校] order by newid())
      

  16.   


    Access 的话试试这个:select * from  #T  t where ID号=(select top 1 ID号 from #T where 学校=t.学校 order by  rnd(time()-ID号))
      

  17.   


    select * from  #T  t where ID号=(select top 1 ID号 from #T where 学校=t.学校 order by  rnd(time()-ID号))
      

  18.   


    access 呀select * from tb t
     where [ID号]=
    (
    select top 1 [ID号]
    from tb 
    where [学校]=t.[学校] 
    order by Rnd(id) 
    )