我试着用一天产生的能量来画一条曲线。所以曲线必须一直在增长
我尝试增加变量"energie_journ"我的mysql数据库与所有以前的。
为例:Mysql:
ID energie_journ
1 5
2 5
3 10
4 6
预期的结果:
First energie_journ = 1, the second = 10 (5 +5), third = 20 (5 +5 +10), fourth = 26 (5 +5 +10 +6).
php:
while ($row2 = mysql_fetch_array($result2)) {
extract($row2);
$datetime *= 1000;
$encode_energie_journ[] = array((float)$datetime, (float)$energie_journ == null ? null : (float)$energie_journ);
}
谢谢。
好了,我已经在查询中创建了以前值的sum
SELECT id,(SELECT SUM(energie_journ) FROM `table` WHERE `id` <= p.`id`) AS sum_col FROM `table` p
只使用sum_col
的结果,我认为这是根据你的需要
希望这是有意义的
试一下:
$energy_journ = 0;
while ($row = mysql_fetch_array($result2)) {
$energy_journ += $row[1];
// now save this value to an array, if that's what you are after.
}
避免使用extract
,它会让你头疼。
你也可以让MySQL为你计算:
SELECT
ID,
energie_journ,
@subtot := @subtot + val AS energie_journ_to_date
FROM myTable, (SELECT @subtot := 0) subtots
ORDER BY ID;
结果如下所示:
ID energie_journ energie_journ_to_date
1 5 5
2 5 10
3 10 20
4 6 26