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

标签 php mysql

提前为表结构道歉...但我有多个表,每个表都与不同用户收集的数据有关。 该数据是连续收集的,并且只是在发生时添加更多行...... 正如我所说,我有多个具有这种相似结构的表,我希望最终得到超过 10 组 5 列的平均值。 (fb1 + fb2 + fb3 + fb4 + fb5)/5 = q 的 1-5 的平均值....

每个列都指的是从网络应用程序调查中得出的答案,并且都是从 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";

附注 因为我在不使用每个人都在谈论的这种 PDO 语法的情况下完成了所有工作,所以我非常感谢除了 PDO 解决方案之外的任何帮助,除非它不可能。

最佳答案

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

首先,查询。我不会那样做的。您的进步很明显,为什么不利用这一点呢?

//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];
}

如您所见,它不适用于复制/粘贴。我假设您在几个不同的地方手动输入所有数据。仅编辑评论说“编辑此内容” 下方的行。其余的评论是解释,不需要进一步的工作。我没有测试过它,但我相信如果您手动输入所有表名并需要我详细说明的字段,它会起作用。

我想我必须告诉你,这不是一个“好的”做法。最好将所有数据放在一个表 values 中,添加一个名为 creator_id 的字段,然后创建另一个名为 creators 的表相同的 creator_id 字段和一个 name 字段。然后使用 WHERE creator_id=$whatever 查看个人调查。

关于php - 遍历Mysql数据库中的所有表并从PHP中的每个表中获取相同列的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114895/

相关文章:

php - 如何在ajax中获得额外的响应?

php - 你可以在 laravel 中的 session 中添加类/id 吗?

mysql HAVING 组用于多个 LIKE 条件?

mysql - 计数总和的最大值

php - SQLite、PDO 和 PHP : New inserts not being saved

php - 从 MySQL 数据库为 Doctrine 生成 YAML 模式或模型

Php Composer openssl错误

php - 将当前日期添加到 DATE 表 mysql 错误

mysql - 记录到文件与记录到数据库

php国家特定时区