关于随机抽取的意思是,每执行一次这个SQL语句,每一组中抽到的一名老师不是同一个人。---------------------------------------------------------------------------------ID号 姓名 学校 65 苏国清 南京市合川小学
406 刘昊斌 南京市合川小学
427 高峰 南京市合川小学 444 庞春云 南京市广盛小学
478 方富贵 南京市广盛小学
480 刘铭 南京市广盛小学
565 杨林 南京市广盛小学 655 张红艳 南京市汉衡小学
921 邹忠臣 南京市汉衡小学 935 吴运新 南京市国联小学
965 赵刚 南京市国联小学
999 王志宏 南京市国联小学
1099 王军 南京市国联小学
2559 王宇 南京市国联小学 290608 丛志波 南京市方正小学 293546 朱东兴 南京市合川小学
406 刘昊斌 南京市合川小学
427 高峰 南京市合川小学 444 庞春云 南京市广盛小学
478 方富贵 南京市广盛小学
480 刘铭 南京市广盛小学
565 杨林 南京市广盛小学 655 张红艳 南京市汉衡小学
921 邹忠臣 南京市汉衡小学 935 吴运新 南京市国联小学
965 赵刚 南京市国联小学
999 王志宏 南京市国联小学
1099 王军 南京市国联小学
2559 王宇 南京市国联小学 290608 丛志波 南京市方正小学 293546 朱东兴 南京市合川小学
SELECT TOP N FROM TABLE ORDER BY NEWID()
我想执行一次SQL语句随机抽取一个,每次抽取的一个都不相同
-->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 個資料列受到影響)
*/
select top 1 from table order by newid()
WHERE NOT EXISTS(SELECT 1 FROM TB WHERE 学校=T.学校 AND ID>T.ID ORDER BY NEWID())
--此方法只适合于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 行受影响)
*/
用随机数rand()比较好
rand()在SQL语句中的具体用法,结合我给出的实例写一下好吗
select * from tb t where [ID号]=(select top 1 [ID号] from tb where [学校]=t.[学校] order by newid())
有时取到的是3条或更多条记录,是加order by newid()的缘故
如何解决啊
select TOP 1 * from tb t where [ID号]=(select top 1 [ID号] from tb where [学校]=t.[学校] order by newid())
Access 的话试试这个:select * from #T t where ID号=(select top 1 ID号 from #T where 学校=t.学校 order by rnd(time()-ID号))
select * from #T t where ID号=(select top 1 ID号 from #T where 学校=t.学校 order by rnd(time()-ID号))
access 呀select * from tb t
where [ID号]=
(
select top 1 [ID号]
from tb
where [学校]=t.[学校]
order by Rnd(id)
)