Guest Posted May 14, 2020 Share Posted May 14, 2020 I create a post just now but cannot seem to find it to add to it. Anyhow, I have 2 tables, I need to multiply qty from the one table to the mass from the other to get a total, which I have achieved with this code: $sql_total_mass = " SELECT jobs_assembly.assemble_qty, jobs.mass, (jobs_assembly.assemble_qty * jobs.mass) AS 'sum' FROM jobs_assembly LEFT JOIN jobs on jobs_assembly.jobs_id = jobs.id LEFT JOIN job_names ON jobs.job_names_id = job_names.job_id WHERE jobs_assembly.assemble_date = '$link_date' ORDER BY job_names.job_name, jobs.assembly "; $result_total_mass = mysqli_query($conn, $sql_total_mass); if (mysqli_num_rows($result_total_mass) > 0) { while($row_total_mass = mysqli_fetch_assoc($result_total_mass)) { echo $row_total_mass['sum'].'<br />'; } //while } //if Now I need to take all these totals and make a grand total. So basically add all the results from $row_total_mass[‘sum’] together and show it. Quote Link to comment https://forums.phpfreaks.com/topic/310775-total-output-of-mysql-sum-query-from-different-columns/ Share on other sites More sharing options...
gw1500se Posted May 14, 2020 Share Posted May 14, 2020 Search engines are your friend. https://www.php.net/manual/en/function.array-sum.php Quote Link to comment https://forums.phpfreaks.com/topic/310775-total-output-of-mysql-sum-query-from-different-columns/#findComment-1577989 Share on other sites More sharing options...
Barand Posted May 14, 2020 Share Posted May 14, 2020 Accumulate the total while you are processing the records $total_mass = 0; while($row_total_mass = mysqli_fetch_assoc($result_total_mass)) { echo $row_total_mass['sum'].'<br />'; $total_mass += $row_total_mass['sum']; } echo 'Total: ' . $total_mass; Quote Link to comment https://forums.phpfreaks.com/topic/310775-total-output-of-mysql-sum-query-from-different-columns/#findComment-1578000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.