从这个table中,随即抽题组成考卷,用存储过程来实现怎么?(补充:这个表里的数据总共八十条,选择、填空、简答、编程各20条。从中抽题组成试卷,选择、填空、简答、编程这四个类型的题都要包括,然后,输出满足条件的ID
要满足:选择*3+填空*2+简答*6+编程*10=100)
随机找出3个选择题:
select top 3 * from table where type="选择" order by newid()
随机找出2个填空题:
select top 2 * from table where type="填空" order by newid()
用存储过程来做啊?
@Nion: 你连存储过程也不会。google一下,成堆,就是相当于一个函数,把这几个sql语句放进去就是的了。
能详细点吗?
楼主的这个题目要求还要解一次多元不定方程~~
楼上给的是MSSQL的取随机三条记录,PL/SQL 应该是
随机找出3个选择题
select a.* from (select a.* from [table] where type ='选择' order by sys_guid() ) a where rownum<=3
随机找出2个填空题
select a.* from (select a.* from [table] where type ='填空' order by sys_guid() ) a where rownum<=2
另外PL/SQL的存储过程写法要求比MSSQL更严格些
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[dnt_adduserapp] @appid int, @uid int, @appname nchar(30), @privacy tinyint, @allowsidenav tinyint, @allowfeed tinyint, @allowprofilelink tinyint, @narrow tinyint, @displayorder smallint, @menuorder smallint, @profilelink text, @myml text AS BEGIN INSERT INTO [dnt_userapp]([appid],[appname],[uid],[privacy],[allowsidenav],[allowfeed],[allowprofilelink],[narrow],[displayorder],[menuorder]) VALUES(@appid,@appname,@uid,@privacy,@allowsidenav,@allowfeed,@allowprofilelink,@narrow,@displayorder,@menuorder) INSERT INTO [dnt_userappfields]([appid],[uid],[profilelink],[myml]) VALUES(@appid,@uid,@profilelink,@myml) END
把参数和语句换成你需要的就行
不用遍历吗,怎么来实现总分=100?
@Nion: 你是说题目数量还不固定???
declare i integer; x1 integer; x2 integer; x3 integer; x4 integer; begin i:=0;
--先算出每种题目需要取多少个 while i<> 100 loop x1:=round(dbms_random.value(1,17)); x2:=round(dbms_random.value(1,17)); x3:=round(dbms_random.value(1,17)); x4:=round(dbms_random.value(1,7)); select x1*3+x2*2+x3*6+x4*10 into i from dual;
--剩下就是取题目了,详见各位楼上,如:
select 题目ID from xx where rownum<=x1 end loop; end;