平均水平数据mysql php

问题描述 投票:0回答:1

我希望你做得好。我在一个学校管理项目工作,我想得到每个科目(Matiere)的5个测试(devoir)的平均值 -

我试图使用AVG()里面获取horizo​​ntale devoir数据的循环,我没有结果,任何建议?

  $query5 = "select * from devoir  where CIN_ELEVE = '$cinE' and ID_ELV_AN_CLS = $select2 GROUP BY MODULE_DEVOIR";
    $result5 = mysqli_query($con,$query5);
    if(mysqli_num_rows($result5)>0)
    {
        echo"<table class='table table-bordered table-striped table-hover table-info'><tr>
        <th>MATIERE</th>
        <th>devoir1</th>
        <th>devoir2</th>
        <th>devoir3</th>
        <th>devoir4</th>
        <th>devoir5</th>
        <th>AVG</th>

        </tr>
        ";
        while($rows = mysqli_fetch_assoc($result5))   
        {
        $moduleD = $rows['MODULE_DEVOIR'];    
        $queryDEV = "select * from devoir inner join eleve on devoir.CIN_ELEVE = eleve.CIN_ELEVE where eleve.CIN_ELEVE = '$cinE' and MODULE_DEVOIR = '$moduleD' order by MODULE_DEVOIR";

        $resultDEV = mysqli_query($con,$queryDEV);
        /*to print subject name*/
            echo"<tr><td>$moduleD</td>";     

               while($rows = mysqli_fetch_assoc($resultDEV))/*loop2*/
                {
                    $noteDEV = $rows['NOTE_DEVOIR'];
        /*to print subject mark(note)*/

                    echo"<td>$noteDEV</td>";

                   /*i add the query of average inside the loop to be with touch by all row data mark*/
             $queryAVG="select avg($noteDEV) as average_devoir from devoir where MODULE_DEVOIR = '$moduleD' group by MODULE_DEVOIR ";
                }

                   $resultAVG = mysqli_query($con,$queryAVG);
                  if($rows = mysqli_fetch_assoc($resultAVG))
                   { 
                      if($rows = mysqli_fetch_assoc($resultAVG))
                            {
                       $avg = $rows['average_devoir'];
                       echo"<td>$avh</td>";
                      }

                   }
            echo"</tr>";
        } 
        echo"</table>";
    } 
php mysql mysqli
1个回答
1
投票

如果您不需要使用SQL来计算平均值,那么这样做似乎是一个更好的选择。

while($rows = mysqli_fetch_assoc($result5))   
{
    $moduleD = $rows['MODULE_DEVOIR'];    
    $queryDEV = "SELECT * FROM devoir INNER JOIN eleve " . 
        "ON devoir.CIN_ELEVE = eleve.CIN_ELEVE " .
        "WHERE eleve.CIN_ELEVE = '$cinE' " . 
        "AND MODULE_DEVOIR = '$moduleD' " .
        "ORDER BY MODULE_DEVOIR";

    $resultDEV = mysqli_query($con,$queryDEV);

    /*to print subject name*/
    echo"<tr><td>$moduleD</td>";

    // We will use this variable to store the sum
    $sum = 0;    

    // And this is a counter to store the number of results
    $count = 0;

    while($rows = mysqli_fetch_assoc($resultDEV))/*loop2*/
    {
        $noteDEV = $rows['NOTE_DEVOIR'];

        /*to print subject mark(note)*/
        echo"<td>$noteDEV</td>";

        // Update the sum
        $sum += $noteDEV;

        // Update the counter
        $count ++;
    }

    // Calculate and display the average
    $avg = $sum / $count;
    echo "<td>$avh</td>" . "</tr>";
}

如果你知道结果的数量总是不变的,并且等于5,你就可以摆脱$count变量并直接除以5。

您可能还希望格式化输出以满足您的需要。

© www.soinside.com 2019 - 2024. All rights reserved.