繁体   English   中英

遍历Mysql数据库中的所有表并从PHP中的每个表中获取相同列的平均值

[英]Loop Through all tables in Mysql database and get Average of identical columns from each table in PHP

预先为表结构表示歉意...但是我有多个表,每个表都与不同用户的收集数据有关。 该数据是连续收集的,并在发生时仅添加更多行...正如我说过的,我有多个具有类似结构的表,最后我希望得到10组5列的平均值。 (fb1 + fb2 + fb3 + fb4 + fb5)/ 5 = q的1-5的平均值....

每一列均指从Web应用程序调查中得出的答案,所有值的取值范围均为1-10。 这些问题以5为一组,然后加在一起,然后以5表示平均值。

我想要的是这个,但是循环遍历所有表中的所有列,并获得更大的相同结果。

我具有每个表的公式,如下所示,但是如何考虑到多个表中的值,如何遍历未设置数量的表以输出相同的值?

    $query="SELECT AVG(fb1), AVG(fb2), AVG(fb3), AVG(fb4), AVG(fb5), AVG(fb6), AVG(fb7), AVG(fb8), AVG(fb9), AVG(fb10), AVG(fb11), AVG(fb12), AVG(fb13), AVG(fb14), AVG(fb15), AVG(fb16), AVG(fb17), AVG(fb18), AVG(fb19), AVG(fb20), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb5), AVG(fb21), AVG(fb22), AVG(fb23), AVG(fb24), AVG(fb25), AVG(fb26), AVG(fb27), AVG(fb28), AVG(fb29), AVG(fb30), AVG(fb31), AVG(fb32), AVG(fb33), AVG(fb34), AVG(fb35), AVG(fb36), AVG(fb37), AVG(fb38), AVG(fb39), AVG(fb40), AVG(fb41), AVG(fb42), AVG(fb43), AVG(fb44), AVG(fb45), AVG(fb46), AVG(fb47), AVG(fb48), AVG(fb49), AVG(fb50) FROM `".$_SESSION['table']."`";

$result=mysql_query($query);

while($row = mysql_fetch_array($result)){
$row1 = $row['AVG(fb1)'] + $row['AVG(fb2)'] + $row['AVG(fb3)'] + $row['AVG(fb4)'] +     $row['AVG(fb5)'];
$row2 = $row['AVG(fb6)'] + $row['AVG(fb7)'] + $row['AVG(fb8)'] + $row['AVG(fb9)'] + $row['AVG(fb10)'];
$row3 = $row['AVG(fb11)'] + $row['AVG(fb12)'] + $row['AVG(fb13)'] + $row['AVG(fb14)'] + $row['AVG(fb15)'];
$row4 = $row['AVG(fb16)'] + $row['AVG(fb17)'] + $row['AVG(fb18)'] + $row['AVG(fb19)'] + $row['AVG(fb20)'];
$row5 = $row['AVG(fb20)'] + $row['AVG(fb22)'] + $row['AVG(fb23)'] + $row['AVG(fb24)'] + $row['AVG(fb25)'];
$row6 = $row['AVG(fb26)'] + $row['AVG(fb27)'] + $row['AVG(fb28)'] + $row['AVG(fb29)'] + $row['AVG(fb30)'];
$row7 = $row['AVG(fb30)'] + $row['AVG(fb32)'] + $row['AVG(fb33)'] + $row['AVG(fb34)'] + $row['AVG(fb35)'];
$row8 = $row['AVG(fb36)'] + $row['AVG(fb37)'] + $row['AVG(fb38)'] + $row['AVG(fb39)'] + $row['AVG(fb40)'];
$row9 = $row['AVG(fb40)'] + $row['AVG(fb42)'] + $row['AVG(fb43)'] + $row['AVG(fb44)'] + $row['AVG(fb45)'];
$row10 = $row['AVG(fb46)'] + $row['AVG(fb47)'] + $row['AVG(fb48)'] + $row['AVG(fb49)'] + $row['AVG(fb50)'];

    $row1Avg = $row1 / 5;
    $row2Avg = $row2 / 5;
    $row3Avg = $row3 / 5;
    $row4Avg = $row4 / 5;
    $row5Avg = $row5 / 5;
    $row6Avg = $row6 / 5;
    $row7Avg = $row7 / 5;
    $row8Avg = $row8 / 5;
    $row9Avg = $row9 / 5;
    $row10Avg = $row10 / 5;

echo " all of those";

ps由于我在不使用所有人都在谈论的PDO语法的情况下完成了所有工作,因此我将非常感谢除PDO解决方案以外的任何帮助,除非不可能。

首先,我将自由更改数组 $ group [1],$ group [2]的$ row1,$ row2 ...,不要将它们与实际行混淆。 它们不是行,实际上是列组。

首先,查询。 我不会那样做。 您显然有进步,那么为什么不利用这个优势呢?

//First, a couple of definitions.
$group=array();
$default=0;     //If there are no results, the default is 0
$k=0;

// The following query looks like this: SELECT AVG(fb1), [...], AVG(fb50) FROM `table`
$queryA="SELECT ";
for ($i=1; $i<50; $i++)
    $queryA.="AVG(fb".$i."), "
$queryA.="AVG(fb50) FROM `table`";
$resultA=mysql_query($queryA);

//I don't know all the other table's names, so I assume table2, table3 etc.
//EDIT THIS. Add the code in brackets as many times as tables (with the respective names $queryC, $queryD... and the $resultC, $resultD...):
{
$queryB="SELECT ";
for ($i=1; $i<50; $i++)
    $queryB.="AVG(fb".$i."), "
$queryB.="AVG(fb50) FROM `table2'`";
$resultB=mysql_query($queryB);
}

$num=0;
//EDIT THIS. Add as many as needed. Gets which result is longer.
if (mysql_num_rows($resultA)>=$num)
    $num=mysql_num_rows($resultA);
if (mysql_num_rows($resultB)>=$num)
    $num=mysql_num_rows($resultB);

//Then, the while loop:
for($a=0; $a<$num; $a++)
    {
    //EDIT THIS. Add as many as needed
    $rowA = mysql_fetch_array($resultA)
    $rowB = mysql_fetch_array($resultB);

    // Repeat it 50 times
    for ($j=0; j<50;j++)
        {
        //EDIT THIS. Add as many as tables suming up.
        $group[$k]+=$rowA["AVG(fb".$j.")"]+$rowB["AVG(fb".$j.")"];

        //EDIT THIS. Get the number to divide for the average
        if (!empty($rowA["AVG(fb".$j.")"])) $group["n".$k]+=1;
        if (!empty($rowB["AVG(fb".$j.")"])) $group["n".$k]+=1;

        //Checks if $j is 4, 9, 14, 19 etc (groups of 5 as 0 is included)
        if ($j%5==4)
            {
            $k++;
            }
    }

for ($i=1; $i<=5; $i++)
{
$group=(float) $group[$i]
$div=(float) $group["n".$i];
if ($div!=0)
    {
    $group[$i]=$group/$div;
    }
else $group[$i]=$default;
echo "<br>Group ".$i.": ".$group[$i];
}

如您所见, 它不适用于copy / paste 我假设您在几个不同的地方手动输入所有数据。 仅在评论说“编辑此”的下方编辑这些行。 其余评论只是解释,不需要进一步的工作。 我尚未测试过,但我相信如果您手动输入所有表名并需要我详细说明的字段,它将可以使用。

以为我必须告诉你,这不是“好的”做法。 这将会是更好的把所有的数据在一个表中values增加一个字段名为creator_id ,并创建另一个表名为creators具有相同creator_id字段和name字段。 然后,您可以使用WHERE creator_id=$whatever来查看各个调查。

暂无
暂无

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

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