Jump to content

Total output of mysql sum query from different columns


Recommended Posts

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.

 

 

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;

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.