2013-05-06 57 views
0

我有這樣一個問題:如何從此前的紀錄在總PHP的MySQL

count this id no 1 = usage/nod 
count this id no 2 = (usage[id1]+usage[id2])/(nod[id1]+nod[id2]) 
count this id no 3 = (usage[id1]+usage[id2]+usage[id3])/(nod[id1]+nod[id2]+nod[id3]) 

等等......

「使用/點頭」是數據庫中的字段。

我如何使用PHP和MySQL來計算?

+0

不應該第一行是'usage [id1]/nod [id1]'? – Barmar 2013-05-06 15:04:13

回答

1
$total_usage = 0; 
$total_nod = 0; 

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
    $total_usage += $row['usage']; 
    $total_nod += $row['nod']; 
    $div = $total_usage/$total_nod; 
    echo "count this id no $row[id] = $div\n"; 
}