如何在php mysql中从上一条记录中获取总计

如何在php mysql中从上一条记录中获取总计,php,mysql,row,record,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]) 等等 “用法/节点”是数据库中的一个字段 使用PHP和MySQL如何计算呢?第一行不应该是usage[id1]/no

我对此有一个问题:

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如何计算呢?

第一行不应该是
usage[id1]/nod[id1]
$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";
}