繁体   English   中英

如何使用表格中的数据显示每列的最大值?

[英]How to display the highest value per column using data from a table?

我的桌子看起来像这样。 这是我的代码,用于显示每个类别的平均值。 但是我想找到每列的最高价值。 像在C1.1中一样,我想在该列中找到最高的平均值。

<div class="container" class="col-lg-5">
<table class="table table-striped table table-bordered" >
<tr><th>Campus</th>
<th>No. of Staff</th>
<th>C1.1</th>
<th>C1.2</th>
<th>C1.3</th>
<th>C1.4</th>
<th>C1.5</th>
<th>C1.6</th>
<th>C2.1</th>
<th>C2.2</th>
<th>C2.3</th>
<th>C3.1</th>
<th>C3.2</th>
<th>C3.3</th>
<th>C3.4</th>

    <tr>
    <td>MAIN 1 - CABEIHM</td>
    <td>
            <?php   
            $queryn ="SELECT COUNT(dept_code) FROM employment where                                     dept_code=3 and empg_code=1";
            $resultn = mysql_query($queryn) or die(mysql_error());
            while($row1 = mysql_fetch_array($resultn)){
            echo "".$row1['COUNT(dept_code)'];
             echo "<br />";
             }
         ?>
    </td>
    <td><!--1.1-->
    <?php

    $query1 = ("SELECT  ROUND(AVG(Compv11),2) , dept_code ,  camp_code
                                FROM performance 
                                INNER JOIN employment 
                                ON employment.emp_code=performance.emp_id AND employment.dept_code=performance.dept_id
                                WHERE empg_code=1  AND dept_id=3
                                ");
                $result1 = mysql_query($query1) or die(mysql_error());
            [enter image description here][1]

                // Print out result
                while($row = mysql_fetch_array($result1))
                {
                echo "".$row['ROUND(AVG(Compv11),2)'];
                }

        ?>

要从C1.1列中获取平均值最高的行,最好的选择是在PHP中进行计算。

从数据库中获取值后,当前您将直接输出这些值。 您需要将平均值存储在变量中,以便以后可以获取最大值:

$averages = array();

$query1 = ("SELECT  ROUND(AVG(Compv11),2) , dept_code ,  camp_code
            FROM performance 
            INNER JOIN employment 
            ON employment.emp_code=performance.emp_id AND employment.dept_code=performance.dept_id
            WHERE empg_code=1  AND dept_id=3
");

$result1 = mysql_query($query1) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result1))
{
    $averages[1] = $row['ROUND(AVG(Compv11),2)'];
    echo "".$row['ROUND(AVG(Compv11),2)'];
}

请注意,我将结果添加到$averages索引1中,因为您的变量分别称为$query1$result1并且我假设对于所有其他行,您都使用递增变量(即第二行的$query2$result2 ,等等。)。 接下来的方法不是很有效,之后我会给您一个替代方法。

一旦所有平均值都在$averages ,就可以对数组进行排序:

arsort($averages);

这将以相反的顺序对数组进行排序(这意味着第一个条目将是具有最高值的条目),同时保持索引与值的关联。 现在我们reset()数组设置为安全的,然后可以使用key()函数获取索引,并使用current()函数获取值本身:

reset($averages);
$highest_index = key($averages);
$highest_value = current($averages);

$highest_query = "query" . $highest_index;
$highest_result = "result" . $highest_index;

现在,您知道最大值来自$$highest_query ,其结果是$$highest_result ,以防您想对该信息进行某些操作。 例如,您可以使用mysql_data_seek($$highest_result, 0)重置它,然后可以使用mysql_fetch_array($$highest_result)再次获取其行。

尽管您应该真正考虑从旧的,不受支持的mysql_*函数转移到mysqliPDO进行数据库通信。

现在,我提到:

根据您要使用的最大值(您说要输出它并“对其进行报告”),您可能还想知道您要在查询中过滤哪个empg_codedept_id ,或者您选择的dept_codecamp_code以及平均值。

因此,让我们在$averages数组中填充的不仅仅是平均值:

while($row = mysql_fetch_array($result1))
{
    $averages[] = array(
        'avg' => $row['ROUND(AVG(Compv11),2)'],
        'empg_code' => 1,
        'dept_id' => 3,
        'dept_code' => $row['dept_code'],
        'camp_code' => $row['camp_code']
    );
    echo "".$row['ROUND(AVG(Compv11),2)'];
}

现在我们可以使用usort通过自定义函数对数组进行排序:

usort($averages, function($a, $b) {
    if ($a['avg'] == $b['avg']) {
        return 0;
    }

    // note: reverse check of the example, because we want to
    // sort in DESCENDING order
    // https://php.net/usort
    return ($a['avg'] > $b['avg']) ? -1 : 1;
});

现在,具有最高值的条目的数据将是$averages数组的第一个条目:

$highest = $averages[0];

echo $highest['avg'];       // highest average value
echo $highest['empg_code']; // 1
echo $highest['dept_id'];   // 3
echo $highest['dept_code']; // dept_code for the highest average value
echo $highest['camp_code']; // camp_code for the highest average value

希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM