随机从某个表中取5条数据,其中表中的id随你的表的记录数而定,我现在测试用的是10000 create table #t( id int,name varchar(50)) declare @random_Id int declare @Count int declare @temp int
set @Count=1 while @Count<=5 begin select @random_Id=cast(10000*rand() as int) if (@random_Id!=0) set @temp=(select top 1 id from person_main where id =@random_Id and person.id not in (select id from #t)) if(@temp is not null) begin insert into #t select top 1 id,name from person_main where id =@random_Id and person.id not in (select id from #t) set @Count=@Count+1 end
再判断一下这个员工是否中过奖,中了再重新来一遍
就可以得到随机数了
create table #t( id int,name varchar(50))
declare @random_Id int
declare @Count int
declare @temp int
set @Count=1
while @Count<=5
begin
select @random_Id=cast(10000*rand() as int)
if (@random_Id!=0)
set @temp=(select top 1 id from person_main where id =@random_Id and person.id not in (select id from #t))
if(@temp is not null)
begin
insert into #t
select top 1 id,name from person_main where id =@random_Id and person.id not in (select id from #t)
set @Count=@Count+1
end
end
select * from #t
drop table #t 全sql方式
对于已经抽过的人
要么给你的表上加一个state字段
要么另外建一个表记录抽奖的记录.
当然,如果是web程序,能保证一直正常运行,存到webapplication里也可以
不过,为了保险起见,新建一个表来保存或者加个字段是比较好的选择.关注下面的朋友更好的方法
Collections.shuffle(list)
把一些员工放到list里面,shuffle下后,直接取前5个就ok,这个是纯java方式,原list把前5个remove掉,在来shuffle,在取前5个,如此往复