mysql数据库随机生成某个范围内的数据

时间:2020-08-14
本文章向大家介绍mysql数据库随机生成某个范围内的数据,主要包括mysql数据库随机生成某个范围内的数据使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、完成内容

1. 根据组卷规则随机生成试题

2. 获取考生答案,计算分数

3. 判断答案正误,错题存到错题表

4. 判断考试是否第一次答题,若不是,使用第一次生成的试题

二、功能截图

 三、核心代码

/**
     * @param context 页面传过来的题号及其对应的答案,形式是 题号:ABCD
     */
    @Override
    public Integer stuTest(String testId, String context, String stuname) {
        // TODO Auto-generated method stub
        //声明一个变量分数
        int rightNum =0;
        String bids="";
        //处理答案,计算分数
        //1.处理文本
        String str[] = context.split(",");
        
        for (int i = 0; i < str.length; i++) {
            
            //获取对应的试题id
            String bid = str[i].substring(0, str[i].indexOf(":"));
            //获取对应答案
            
            String answer = str[i].substring(str[i].indexOf(":")+1);
            
            //根据bid查询出题库中正确的答案
            ItemBank itemBank = itemBankDao.selectByBid(new Integer(bid));
            //判断答案是否一致
            answer = answer.replaceAll(" ", "");
            String itemBankAnswer = itemBank.getAnswer().replace(" ", "");
            
            if(answer.equalsIgnoreCase(itemBankAnswer)) {
                
                rightNum=rightNum+1;
                System.out.println("rightNum>>>"+rightNum);
            }else {
                
                //查询判断是否含有此bid
                MisCollection collection = misCollectionDao.selectByStuname(new Integer(stuname));
                if(collection!=null) {
                    String misbids = collection.getBids();
                    if(!misbids.contains(bid)) {
                        misbids+=","+bid;
                        misCollectionDao.update(misbids,new Integer(stuname));
                        
                    }
                    
                }else {
                    //第一次做题
                    
                        if(i==0) {
                            bids=bid;
                        }else {
                            bids+=","+bid;
                        }
                        
                        //将错题存到表中
                        misCollectionDao.add(bids,new Integer(stuname));
                }
                
            }
            
        }
        
        
        
        
        Double score =  (double)((rightNum/str.length)*100);
        //将分数,答案更新到试卷表(testpaper)
        return testPaperDao.updateScoreAndAnswer(score,context,new Integer(testId),new Integer(stuname));
        
         
    }

四、数据库随机范围查询

<!-- 随机生成试卷 -->
 <select id="makePaper" resultMap="itemBank">
 SELECT * FROM itembank
    WHERE bid >= (SELECT floor((select MIN(bid) from itembank where subid=#{0})+RAND() * (SELECT MAX(bid) FROM itembank where subid=#{0}))) 
    ORDER BY bid LIMIT #{1}
  
 </select>

原文地址:https://www.cnblogs.com/wangpeng94/p/13501442.html