I want to get sum of the prices and items columns which are related to 3 tables.I am trying with INNER JOIN
in the following script. It only displays 1 invoice total. And else statement is not displaying 0 When data is 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";
}
}
}
If you have multiple SUM function, you should probably use the GROUP BY instruction. See below
https://www.w3schools.com/sql/sql_groupby.asp
https://www.w3resource.com/sql/aggregate-functions/sum-with-group-by.php