用php求上一个值的和
我如何创建一个函数,它可以将上一行与新行相加,并显示每行的结果 如“SALDO”一栏中所示: tExiste是产品数量的总和这是所有存在的总和减去每个产品的所有销售额之和用php求上一个值的和,php,mysql,sum,Php,Mysql,Sum,我如何创建一个函数,它可以将上一行与新行相加,并显示每行的结果 如“SALDO”一栏中所示: tExiste是产品数量的总和这是所有存在的总和减去每个产品的所有销售额之和 $sql = "SELECT nombreProd, compra, venta, dnm, SUM(existencia - vendido) AS tExiste, (SUM(existencia - vendido) * compra) AS Tott FROM PRODUCTOS WHERE f_producto
$sql = "SELECT nombreProd, compra, venta, dnm,
SUM(existencia - vendido) AS tExiste,
(SUM(existencia - vendido) * compra) AS Tott
FROM PRODUCTOS WHERE f_producto BETWEEN
'".$fromdate."' AND '".$todate."' GROUP BY code
ORDER BY id DESC";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '
<tbody>
<tr>
<td>'.$row["tExiste"].'</td>
<td class="center">'.$row["nombreProd"].'</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["compra"].'
</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["dnm"].'
</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["venta"].'
</td>
<td>
<span class="add-on">'.$moneda.' </span>'.$row["Tott"].'
</td>
<td>
<span class="add-on">'.$moneda.' </span>
</td> // Here is where I need show the sum of each previous row with the new one
</tr>
</tbody>
'; }
Tott是总购买量这是所有存在量的总和减去每个产品的所有销售额的总和,结果乘以每个产品的购买价格
$sql = "SELECT nombreProd, compra, venta, dnm,
SUM(existencia - vendido) AS tExiste,
(SUM(existencia - vendido) * compra) AS Tott
FROM PRODUCTOS WHERE f_producto BETWEEN
'".$fromdate."' AND '".$todate."' GROUP BY code
ORDER BY id DESC";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '
<tbody>
<tr>
<td>'.$row["tExiste"].'</td>
<td class="center">'.$row["nombreProd"].'</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["compra"].'
</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["dnm"].'
</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["venta"].'
</td>
<td>
<span class="add-on">'.$moneda.' </span>'.$row["Tott"].'
</td>
<td>
<span class="add-on">'.$moneda.' </span>
</td> // Here is where I need show the sum of each previous row with the new one
</tr>
</tbody>
'; }
$sql=“选择nombreProd、compra、venta、dnm、,
作为tExiste的SUM(existencia-vendido),
(金额(存在-卖方)*COMPA)合计
从producto到f_producto
“$fromdate.”和“'.$todate.”按代码分组
按id描述订购”;
$result=$conn->query($sql);
而($row=$result->fetch(PDO::fetch_ASSOC)){
回声'
“.$row[“tExiste”]”
“.$row[“nombreProd”]”
“.$moneda.”..$row[“COMPA”]。”
“.$moneda.”..$row[“dnm”]。”
“.$moneda.”.$row[“文塔”]。”
“.$moneda.”.$row[“Tott”]。”
“.$moneda。”
//在这里,我需要显示前一行与新行的总和
'; }
最后,我需要显示那些SUM
编辑 我需要将上一个Tott加上下一个Tott(Tott+Tott),而不是存在与总数的总和(tExiste+Tott) e、 g:R1:84+R2:900=984+R3:150=1134…等
谢谢你如果我是你,我会用PHP对所有需要求和的值进行简单求和,然后
echo
(我没有遵循你需要求和的值,所以我给了你一个例子)。我总是建议,如果可能的话,你应该让PHP来做这项工作,而不是你的数据库
$prev = 0;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '
<tbody>
<tr>
<td>'.$row["tExiste"].'</td>
<td class="center">'.$row["nombreProd"].'</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["compra"].'
</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["dnm"].'
</td>
<td class="center">
<span class="add-on">'.$moneda.' </span>'.$row["venta"].'
</td>
<td>
<span class="add-on">'.$moneda.' </span>'.$row["Tott"].'
</td>
<td>
<span class="add-on">'.$moneda.' </span>
</td>
<td>' . ($row["Tott"] + $prev) . '</td>
</tr>
</tbody>
';
$prev = $row["Tott"];
}
$prev=0;
而($row=$result->fetch(PDO::fetch_ASSOC)){
回声'
“.$row[“tExiste”]”
“.$row[“nombreProd”]”
“.$moneda.”..$row[“COMPA”]。”
“.$moneda.”..$row[“dnm”]。”
“.$moneda.”.$row[“文塔”]。”
“.$moneda.”.$row[“Tott”]。”
“.$moneda。”
“.($row[“Tott”]+$prev)。”
';
$prev=$row[“Tott”];
}
您可以使用SQL获取总数
从文件
SELECT IFNULL(year,"Total") as year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP;
+-------+-------------+
| year | SUM(profit) |
+-------+-------------+
| 2000 | 4525 |
| 2001 | 3010 |
| Total | 7535 |
+-------+-------------+
在while中存储当前的“total”值,并在行的末尾解析它。在while结束之前,将该值增加您最后添加的金额。$row[“Tott”]是html表的“Total Compra”列吗?@briosheje是的,你可以在mysql中使用用户变量来实现它,这很难看,但也是可能的。是的,很容易做到这一点+1@machavity,我需要将上一个Tott加上下一个Tott(Tott+Tott),而不是存在与总数的总和(tExiste+Tott)。。。e、 g:84+900=984@user3236149我已经调整了我的答案that@Machavity谢谢你,那是!!但这是非常罕见的,因为当我把
$prev=0代码>在第二行显示0(900+0=900),但当我更改为$prev=$prev代码>工作。。(900+84=984)…谢谢你,但我需要把上一个Tott加上下一个Tott(Tott+Tott),而不是存在与总数的总和(tExiste+Tott)。。。e、 g:84+900+150=1134。。。等