如何从php mysql中的前一条记录中获取总数

时间:2013-05-06 14:52:25

标签: php mysql row record

我有一个问题:

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])

等等......

“usage / nod”是数据库中的一个字段。

我如何用PHP和MySQL来计算?

1 个答案:

答案 0 :(得分: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";
}