簡體   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