请教从db表随机抽取一些记录的思路?
要求:
1.随机抽取n条
2.每次sql执行结果不一样,n条数据不是连续的
3.如果后台不能实现,那结合前台如何做
4.避免将所有数据取到前台,再随机抽取
[解决办法]
create table db(id int identity(1,1),name varchar(50))
insert into db select 'qqq '
union all select 'www '
union all select 'eee '
union all select 'rrr '
union all select 'asdf '
union all select '35t '
union all select 'aha465 '
union all select '789gf9 '
declare @var int
select @var = cast(rand()*count(1)+1 as int) from db
exec( 'select top '+@var+ ' * from db order by newid() ')
[解决办法]
select top n * from table_name
order by newid()