【发布时间】:2016-12-09 11:46:04
【问题描述】:
我有一个包含profit 列的报告。我需要添加利润列的所有值并获得当天的总利润。感谢任何帮助,因为我已经完成了 SO,但没有找到解决方案。
$result = mysqli_query($connection, "SELECT S.sales_id, I.item_name, ST.stock_code,
S.date, S.unit_price, SUM(S.qty), (SUM(S.qty)*S.unit_price),
((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)) FROM sales S
INNER JOIN items I ON S.item_id=I.item_id
INNER JOIN stock ST ON S.stock_id=ST.stock_id
INNER JOIN purchase_items P ON S.purchase_id=P.purchase_id
WHERE S.date BETWEEN '$date 00:00:00' AND '$date 23:59:59' GROUP BY S.item_id");
$qty = $row['SUM(S.qty)'];
$unit_price = $row['unit_price'];
$amount = $row['(SUM(S.qty)*S.unit_price)'];
$profit = $row['((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost))'];
$dailyProfit = $row['(SUM(SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)))'];
$pdf->Cell(25,10,$dailyProfit,'B',0,'J',1);
这仅检索一条记录,最后一项为profit,应如何更正以获得每日sum(profit)?
更新代码
$result = mysqli_query($connection, "SELECT S.sales_id, I.item_name, ST.stock_code, S.date,
S.unit_price, SUM(S.qty) AS line_qty, (SUM(S.qty)*S.unit_price) AS line_total,
((SUM(S.qty)*S.unit_price)-(SUM(S.qty)*P.unit_cost)) AS line_profit
FROM sales S
INNER JOIN items I ON S.item_id=I.item_id
INNER JOIN stock ST ON S.stock_id=ST.stock_id
INNER JOIN purchase_items P ON S.purchase_id=P.purchase_id
WHERE S.date BETWEEN '$date 00:00:00' AND '$date 23:59:59' GROUP BY S.item_id");
$qty = $row['line_qty'];
$unit_price = $row['unit_price'];
$amount = $row['line_total'];
$profit = $row['line_profit'];
$aRows = array();
while ($oRow = mysqli_fetch_assoc($result)) $aRows[] = $oRow;
mysqli_free_result($result);
$dTotalProfit = 0;
if (count($aRows) > 0) foreach ($aRows as $aRow) {
$dTotalProfit += $aRow['line_profit'];
//$pdf->Cell(25, 10, $aRow['line_qty'], 'B', 0, 'J', 1);
}
$totalProfit = number_format($dTotalProfit, 2);
$pdf->Cell(220, 10, 'Daily Total Profit:', 'B', 0, 'R', 1);
$pdf->Cell(25, 10, $totalProfit, 'B', 0, 'J', 1);
【问题讨论】:
-
您的代码向我们展示了您到目前为止所尝试的内容?
-
啊抱歉我的错……现在可以看到了。
-
我认为您确实按 item_id 分组,因此您将根据该特定项目获得一条记录的结果,因此我认为您应该在此之后使用 array_sum() 来计算全天利润
-
@jilesh 感谢您的解决方案。感谢您是否可以帮助我构建代码。
标签: php html mysql report fpdf