请教一个今天遇到的关于随机出试题的SQL语句

西川痱子 2012-09-27 02:04:13
加精
今天去笔试:遇到一个题目,题目大概是这样的:
有一个试题表:T_EXAM {ID,type(1,2,3/表示试题类型),difficulty(1,2/试题难度),distinguish(1,2/区分)}
现在我要从题库随机抽出20道题,type:类型1的6道,2的7道,3的7道;difficulty:难度1的8道,2的12道;distinguish:区分1的13道,2的7道:请问能用SQL查询出来吗?
如果能用SQL查询出来,SQL语句该怎么写?
...全文
5886 139 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
139 条回复
切换为时间正序
请发表友善的回复…
发表回复
tel18355100713 2013-02-27
  • 打赏
  • 举报
回复
引用 2 楼 汤姆克鲁斯 的回复:
简单的写应该是这样的,期待更牛逼的算法。 SQL code ? 1234567 select top 6 * from tb where type=1 and difficulty=1 and distinguish=1 oder by newid() union allselect top 2 * from tb where type=2 and diffic……
我怎么执行这个sql是错误的啊???版本是2008
wslejeff 2013-02-27
  • 打赏
  • 举报
回复
select * from ( select * from T_EXAM where id in( SELECT id FROM (select id from T_EXAM where type = 1 ORDER BY dbms_random.random) WHERE ROWNUM<=6 union all SELECT id FROM (select id from T_EXAM where type = 2 ORDER BY dbms_random.random) WHERE ROWNUM<=7 union all SELECT id FROM (select id from T_EXAM where type = 3 ORDER BY dbms_random.random) WHERE ROWNUM<=7 ) and id in ( SELECT id FROM (select id from T_EXAM where difficulty = 1 ORDER BY dbms_random.random) WHERE ROWNUM<=8 union all SELECT id FROM (select id from T_EXAM where difficulty = 2 ORDER BY dbms_random.random) WHERE ROWNUM<=12 ) and id in ( SELECT id FROM (select id from T_EXAM where distinguish = 1 ORDER BY dbms_random.random) WHERE ROWNUM<=13 union all SELECT id FROM (select id from T_EXAM where distinguish = 2 ORDER BY dbms_random.random) WHERE ROWNUM<=7 ) ORDER BY dbms_random.random ) WHERE ROWNUM<=20
  • 打赏
  • 举报
回复
不赞成top,虽然能够出来结果,但是没有随机性,建议用in或者exists就随机性
霜之哀伤额 2012-10-11
  • 打赏
  • 举报
回复
整合了一下楼上大牛们的,直接执行即可,方便我们这种菜菜
DECLARE @T_EXAM table(
id int ,
[type] int,
difficurity int,
distinguish int);

insert into @T_EXAM(id,[type],difficurity,[distinguish])
select top 10000 row_number()over(order by newid()), abs(checksum(newid())%3)+1,abs(checksum(newid())%2)+1,abs(checksum(newid())%2)+1 from master.dbo.spt_values a

DECLARE @t table(
id int ,
[type] int,
[difficurity] int,
[distinguish] int);

declare @type1 int
declare @type2 int
declare @type3 int
declare @difficulty1 int
declare @difficulty2 int
declare @distinguish1 int
declare @distinguish2 int

set @type1=1
set @type2=2
set @type3=3
set @difficulty1=1
set @difficulty2=2
set @distinguish1=1
set @distinguish2=2

declare @i int
set @i = 1

while (@i <= 20)
begin

if (select COUNT(*) from @t where [TYPE]=1) = 6
set @type1 = 0
if (select COUNT(*) from @t where [TYPE]=2) = 7
set @type2 = 0
if (select COUNT(*) from @t where [TYPE]=3) = 7
set @type3 = 0
if (select COUNT(*) from @t where [difficurity]=1) = 8
set @difficulty1=0
if (select COUNT(*) from @t where [difficurity]=2) = 12
set @difficulty2=0
if (select COUNT(*) from @t where [distinguish]=1) = 13
set @distinguish1=0
if (select COUNT(*) from @t where [distinguish]=2) = 7
set @distinguish2=0

insert into @t
select top 1 *
from @T_EXAM te
where [TYPE] in (@type1,@type2,@type3)
and [difficurity] in (@difficulty1,@difficulty2)
and [distinguish] in (@distinguish1,@distinguish2)
and not exists(select * from @t t2 where te.[id]=t2.[id])
order by newid()

if @@RowCount = 0 --已没有符合记录
break;
else
set @i = @i + 1
end

select * from @t
霜之哀伤额 2012-10-11
  • 打赏
  • 举报
回复
[Quote=引用 85 楼 的回复:]

SQL code


create table #t(
id int ,
[type] int,
[difficurity] int,
[distinguish] int);


declare @type1 int
declare @type2 int
declare @type3 int
declare @difficulty1 int
declare @……
[/Quote]

85楼的开头定义改成 DECLARE @t table(
id int ,
[type] int,
[difficurity] int,
[distinguish] int);
这样感觉比较好- -,而且while循环里面应该是#t,85楼确实给力
  • 打赏
  • 举报
回复
烦死了
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

简单的写应该是这样的,期待更牛逼的算法。
SQL code
select top 6 * from tb where type=1 and difficulty=1 and distinguish=1 oder by newid()
union all
select top 2 * from tb where type=2 and difficulty=1 and distinguish……
[/Quote]怎么这么流弊···
akemi_homura 2012-10-10
  • 打赏
  • 举报
回复
吾肿么愚昧的觉得10楼的只有19道题。。嘛。当然这不是重点了。 学习了。
[Quote=引用 10 楼 的回复:]
SQL code





create table #tb(
id int ,
[type] int,
[difficurity] int,
[distinguish] int);

insert into #tb select top 1 * from T_EXAM order by NEWID()

declare @……
[/Quote]
Jackile 2012-10-10
  • 打赏
  • 举报
回复
2楼的代码很精简~
专坑队友 2012-10-09
  • 打赏
  • 举报
回复
学习了 膜拜牛人中
s19850904 2012-10-09
  • 打赏
  • 举报
回复
路过,学习,是数据库的哦
dpchy 2012-10-09
  • 打赏
  • 举报
回复
学习中了,请问有没学习SQL的群呢!
txzsp 2012-10-09
  • 打赏
  • 举报
回复
LZ好像不应该用union all,而应该用union,由于是随机取的记录可能用union all会产生重复记录,这样的话是不是就违背了题目的本意了?
zxxhz 2012-10-09
  • 打赏
  • 举报
回复
加强学习中!
huaan011 2012-10-09
  • 打赏
  • 举报
回复
不错!
samyou 2012-10-08
  • 打赏
  • 举报
回复
懂得运用算法都是牛人。
bingquan12 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 118 楼 的回复:]

引用 117 楼 的回复:
引用 98 楼 的回复:

10L,85L解法都不错,85L的更好一点。。。。。


我怎么没有看出来 85L是随即的? 同样的数据,每次运行是不一样的结果吗?


好吧,我试验了,果然不同。为什么?求解释!
[/Quote]

order by newguid() ... 学习了!
bingquan12 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 117 楼 的回复:]
引用 98 楼 的回复:

10L,85L解法都不错,85L的更好一点。。。。。


我怎么没有看出来 85L是随即的? 同样的数据,每次运行是不一样的结果吗?
[/Quote]

好吧,我试验了,果然不同。为什么?求解释!
bingquan12 2012-10-08
  • 打赏
  • 举报
回复
[Quote=引用 98 楼 的回复:]

10L,85L解法都不错,85L的更好一点。。。。。
[/Quote]

我怎么没有看出来 85L是随即的? 同样的数据,每次运行是不一样的结果吗?
blackkettle 2012-10-08
  • 打赏
  • 举报
回复
强悍。学习了!
加载更多回复(95)

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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