通过此查询获得的订单总数:
<?php $res = mysqli_query($conn,'SELECT sum(totalprice) FROM orders');
if (FALSE === $res) die("Select sum failed: ".mysqli_error);
$row = mysqli_fetch_row($res);
$sum = $row[0];
echo $sum;?>
通过此查询获得的费用表总计:
<?php $res = mysqli_query($conn,'SELECT sum(amount) FROM expenses');
if (FALSE === $res) die("Select sum failed: ".mysqli_error);
$row = mysqli_fetch_row($res);
$sum = $row[0];
echo $sum;?>
现在我想从订单和费用表中减去总数。 示例订单总计-费用总计
答案 0 :(得分:0)
如果您提到的sript工作正常,则可以执行以下操作:
$resOrder = mysqli_query($conn,'SELECT sum(totalprice) FROM orders');
if (FALSE === $resOrder) die("Select sum failed: ".mysqli_error);
$row = mysqli_fetch_row($resOrder);
$total = $row[0];
echo $orderTotal;
$resExp = mysqli_query($conn,'SELECT sum(amount) FROM expenses');
if (FALSE === $resExp) die("Select sum failed: ".mysqli_error);
$row = mysqli_fetch_row($resExp);
$expensesTotal = $row[0];
echo $expensesTotal ;
现在要获得结果,就像:
echo $result = $expensesTotal - $orderTotal;
如果遇到任何问题,请告诉我。
答案 1 :(得分:0)
您是如此亲密。您只需要为总值赋予不同的变量名即可。
<?php
$orderRes = mysqli_query($conn,'SELECT sum(totalprice) as orders_total FROM orders'); //Aliases are a beautiful thing to use. In this case I am giving the sum of orders an alias - orders_total
$row = mysqli_fetch_assoc($orderRes);
$ordersTotal = $row['orders_total'];
$res = mysqli_query($conn,'SELECT sum(amount) as expenses_total FROM expenses');
$row = mysqli_fetch_assoc($res);
$expensesTotal = $row['expenses_total']
$diff = $ordersTotal - $expensesTotal; //Simply substract expensesTotal from ordersTotal
echo $diff; ?>