doufu8887 2017-05-23 06:38
浏览 61
已采纳

返回mysql中单列总和等于“16”的行

my project is a question paper generator for each question mark may varies based on the question type, i need to filter out some random question where based on the mark field

eg

only one table

Questions | Mark
q1        | 4
q2        | 4
q3        | 8
q4        | 6
q5        | 12
q6        | 2

i want to select the rows when we SUM(mark) is equal to 16

the result may varie like as follows

q1   | 4
q2   | 4
q4   | 6
q5   | 2

when we sum this result we will get 16 like this i need a query

my query is

Select question, mark 
from table 
where sum(mark) = 16
  • 写回答

1条回答 默认 最新

  • dongmi5015 2017-05-23 09:09
    关注

    The effort will be made by the computer, but if you have a large amount of data, take a coffee in the meantime.

    /* ===== TAKEN FROM PHP.net ====== */
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error);
        exit();
    }
    
    $query = "Select question, mark from table"; // your query
    
    $arrayData = array();//set an empty array to populate
    
    if ($result = $mysqli->query($query)) {
    
        /* fetch associative array */
        while ($row = $result->fetch_assoc()) {
    
            //for calculation purpose, we force the Mark field to be an integer
            $arrayData[$row['question']] = (int)$row['mark']; //populate array with sql results
        }
    
        /* free result set */
        $result->free();
    }
    
    /* close connection */
    $mysqli->close();
    

    Now we have the SQL results in the $arrayData variable. Like this:

    Array("q1"=>4, "q2"=>4, "q3"=>8)
    

    and so on...

    Let's go.

     $targetSUM = 16;
    //First of all, remove any doubt and see 
    //if the sum of the entire array is the same as our target
    
    if($targetSUM === array_sum($arrayData)){
        print_r($arrayData);
        die();
    }
    $i=0;
    $results = array();
    foreach($arrayData as $questionA=>$markA){
    
        $thisCycle = array($questionA=>$markA);
        foreach($arrayData as $questionB=>$markB){
            if ($questionA === $questionB) continue; // avoid sum SUM itself
    
            $thisCycle[$questionB] = $markB;
    
            $thisCycleSUM = array_sum($thisCycle);
    
            switch(true){
                case $thisCycleSUM < $targetSUM: //if results not reach our target, go on
                    continue;
                break;
                case $thisCycleSUM === $targetSUM: //if results match, store and go on
                   $results[$i] = $thisCycle;
                   $thisCycle = array($questionA=>$markA);
                break;
                case $thisCycleSUM > $targetSUM: //if results exceeds, remove last element and go on
                    unset($thisCycle[$questionB]);
                break;
            }
    
        }
    
    
            // Sorting procedure to avoid same combinations and no-combination
        if(isset($results[$i])){ 
            ksort($results[$i]); 
            $results[implode(array_keys($results[$i]))] = $results[$i];
            unset($results[$i]);
            $i++;
        }
    
    }
        $results = array_values($results);
       print_r($results);
    

    DEMO HERE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图