如何随机抽取N条记录

mingjunr 2003-08-19 02:20:31
如何随机抽取N条记录?
回答:select top 10 * from 表 order by newid()的没有分,我想要 的是其它的方法!!
...全文
52 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
db1x2y3z 2003-08-19
  • 打赏
  • 举报
回复
select top 10 * from 表 order by newid()

这样为什么不行?


top 方法中,,top 10 后面不能直接用*或者为空,必须加详细列名
hjb111 2003-08-19
  • 打赏
  • 举报
回复
这样做很麻烦!
hjb111 2003-08-19
  • 打赏
  • 举报
回复
create proc P_RandRecord @tablename varchar(25)
as
exec('select identity(int,1,1) as idd,* into #t from '+@tablename)
select * into #t1 from #t where 1=2
declare @n int
set @n=0
while @n<>10
begin
insert into #t1 select * from #t where idd=(cast(rand()*100) as int)
@n=@n+1
end
select distinct top 10 * from #t1
CrazyFor 2003-08-19
  • 打赏
  • 举报
回复
select top 10 * from 表 order by newid()

这样为什么不行?
hjb111 2003-08-19
  • 打赏
  • 举报
回复
试了用rand()没有做到比newid()好一点!
CrazyFor 2003-08-19
  • 打赏
  • 举报
回复
select top 10 * from 表 order by newid()
愉快的登山者 2003-08-19
  • 打赏
  • 举报
回复
没有比ORDER BY NEWID()更简单的方法了。

愉快的登山者


◢◣◢◣◢◣

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧