我想获得与3个表相关的价格和项目列的总和。我正在尝试使用以下脚本中的INNER JOIN
。它仅显示1个发票总额。否则语句不显示0当数据为0时。
function TotalRecord($total){
global $db;
$date = date("Y-m-d");
$query = $db->prepare("SELECT sum(a.prices), sum(a.item) FROM items a INNER
JOIN invoice_items b ON a.item_id = b.item_id INNER JOIN invoices c ON
b.invoiceId = c.invoiceId WHERE c.invoice_date = :date ");
$query->bindParam(':date', $date);
$query->execute();
for($i=0;
$rows = $query->fetch();
$i++){
$totalPrice = $rows['sum(a.prices)'];
$totalitems = $rows['sum(a.item)'];
$array = array('total' => $totalPrice , 'items' => $totalitems );
if(count($array) > 0)
{
echo $array[$total];
}
else
{
echo "0";
}
}
}
答案 0 :(得分:2)
如果您有多个SUM函数,则应该使用GROUP BY指令。见下文
https://www.w3schools.com/sql/sql_groupby.asp
https://www.w3resource.com/sql/aggregate-functions/sum-with-group-by.php
答案 1 :(得分:0)
试试这个
SELECT sum(a.prices), sum(a.item) FROM (items a INNER
JOIN invoice_items b ON a.item_id = b.item_id INNER JOIN invoices c ON
b.invoiceId = c.invoiceId ) group by b.item_id,b.invoiceid WHERE c.invoice_date = :date