我现在在做一个在线考试系统,要从题目表singleproblem(假设题目表里面有100个题目)随机取出50个题目,结构如下:
  id number not null primary key,
  title varchar2(500) not null,
  answer varchar2(500) not null,
  rightanswer varchar2(500) not null,
  state number not null,//1代表简单 2代表中级 3代表高级
  type number not null,//1代表选择题 2代表多选题 3代表判断题
  testid number题目有3种类型(type),现在我要取N个选择题,M个多选题,K个判断题,然后 N+M+K=50,就是根据一定的概率在50个题目中随机取,怎么做啊?高手求救。(不仅要随机,还要按比例)

解决方案 »

  1.   

    从程序里面设定三种题目的比例,然后根据比例得出每种的数量,然后分别查询三种
    比如说随机获取简单的题目五道:
    select top 5 * from singleproblem where state=1  order by newid()
      

  2.   

    随机循环着抽,数量够了就下一个type,重复了重新抽。
      

  3.   

    分别根据type 先group by,然后对每一组数据随机取指定数量的记录
      

  4.   

    打完收工select * FROM (select top 15 *, NewID() as random from singleproblem  where  fieldname='1' order by random)
    as aa
    union all 
    select * FROM ( select top 20 *, NewID() as random from singleproblem  where  [type]='2' order by random ) 
    as bb
    union all 
    select * FROM ( select top 15 *, NewID() as random from singleproblem  where  [type]='3' order by random ) 
    as cc
      

  5.   

    第一句字段写错了,不好意思。。
    select * FROM (select top 15 *, NewID() as random from singleproblem  where  [type]='1' order by random)
    as aa
      

  6.   

    create proc SP_OnlineScore
    @N int ,
    @M int,
    @K int 
    as 
    begin
     if((@N+@M+@K)>50)
     return
     select top (@N+@M+@K)* from 
     (
     select * from tble
          where [type]=1
     union all
     select * from tble 
          where [type]=2
     union all 
     select * from tble 
          where [type]=3
      )
      as T0
    end
    Go
      

  7.   

    niuzai520create proc SP_OnlineScore
    @N int ,
    @M int,
    @K int  
    as  
    begin
     if((@N+@M+@K)>50)
     return
     select top (@N+@M+@K)* from  
     (
    select * from tble
    where [type]=1
    union all
    select * from tble  
    where [type]=2
    union all  
    select * from tble  
    where [type]=3
      )
      as T0
    end
    Go
    可不可以帮我把语句写出来,不用存储过程???非常感谢
      

  8.   


    <p style = "color:#f00"> up++ </p>
      

  9.   

    oracle类似这样的,,如果你取的记录数也要控制,,那最好在代码中取随机数,然后分别赋值就好。。因为这样,sql代码最简单,也容易理解。 select * FROM (select * from singleproblem  where  [type]='1' order by dbms_random.value  ) where rownum <= 15 
      

  10.   


    看来你还是没理解,,我用oracle只是写了一个类型为1的查询,,你把三条sql语句union all一下不就可以了。。还有随机共取50条记录,,那这个随机数在代码控制
      

  11.   

    我是新手啊,这个union all还没用过,不知道是什么东东,随机数在rownum那儿不能控制么?