怎样在表中随机选择5个记录?(急,在线等)

manonroad 2004-11-13 06:41:25
假设有一个表
questions
结构如下:
QID(pk)
CONTENT
CATEGORYid(FK)
我现在想随机选择5个questions在选定的category(假设categoryid是001),
有没有对应的sql语句实现这个功能?
...全文
216 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yown 2004-11-16
  • 打赏
  • 举报
回复
随机newid()排序
====================================

select top 5 * from questions where categoryid='001' order by newid()
y81920 2004-11-16
  • 打赏
  • 举报
回复
select top 5 * from questions where categoryid=001 order by newid()
davorsuker39 2004-11-16
  • 打赏
  • 举报
回复
insert 另一表
select top 5 QID from questions
where categoryid='001'
order by newid()
qizhanfeng 2004-11-16
  • 打赏
  • 举报
回复
up
lyzzbbgo 2004-11-15
  • 打赏
  • 举报
回复
来太晚了 :(
deodarsydn 2004-11-15
  • 打赏
  • 举报
回复
UP
victorycyz 2004-11-13
  • 打赏
  • 举报
回复
insert TableName (ID)
select * from ( select top 5 QID
from Questions
where Categoryid='001'
order by newid()
) a
union all
select * from ( select top 10 QID
from Questions
where Categoryid='002'
order by newid()
) b
union all
select * from ( select top 15 QID
from Questions
where Categoryid='003'
order by newid()
) c
laoQ 2004-11-13
  • 打赏
  • 举报
回复
insert into YourTableName
select top 5 qID from questions where categoryid=001 order by newid()

唉,,
又来晚了。。
manonroad 2004-11-13
  • 打赏
  • 举报
回复
谢谢楼上各位,还有个问题就是我是在写一个在线考试系统(用asp)。
我可能会从不同的category里选questions。
假设:从001里选5题,002里选10题,003里选15题,
sql语句是怎样?

如果你懂asp能否教我一下?我想把我要选的问题选出来后,生成一组试题。然后我每一页显示一题,怎样处理?
我还可以开贴给分。
lzymagi 2004-11-13
  • 打赏
  • 举报
回复


select top 5 questions from questions where category='001' order by newid()

值返回就用 select into

lyzzbbgo 2004-11-13
  • 打赏
  • 举报
回复
多了一行
select top 5 QID
insert into #t
from questions
where categoryid='001'
order by newid()

select * from #t
drop table #t
lyzzbbgo 2004-11-13
  • 打赏
  • 举报
回复
insert 另一表
select top 5 QID
insert into #t
from questions
where categoryid='001'
order by newid()

select * from #t
drop table #t
Frewin 2004-11-13
  • 打赏
  • 举报
回复
select top 5 * from questions where categoryid=001 order by newid()
victorycyz 2004-11-13
  • 打赏
  • 举报
回复
insert TableName (ID)
select top 5 QID
from Questions
where Categoryid='001'
order by newid()
Andy__Huang 2004-11-13
  • 打赏
  • 举报
回复
随机选择,要加newid()做排序

select top 5 * from questions where categoryid='001' order by newid()
zjcxc 元老 2004-11-13
  • 打赏
  • 举报
回复
insert 另一表
select top 5 QID from questions
where categoryid='001'
order by newid()
wenhongL 2004-11-13
  • 打赏
  • 举报
回复
insert into tab(id)
select top 5 qID from questions where categoryid=001 order by newid()
manonroad 2004-11-13
  • 打赏
  • 举报
回复
怎样把值返回到另一张表中(仅返回QID)
mgsray 2004-11-13
  • 打赏
  • 举报
回复
select top 5 * from questions where categoryid=001 order by newid()

34,593

社区成员

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

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