Sum of selected fields.<

liunx

Guest
Hey.
I have a sql database which contains more than 100 companies. All these companies have a number of employees.
That number of employees is listed in 1 field per company.

Users can select through a php system what company or group of companies they want to get information from.
SO, sometimes 10 companies are selected, sometimes 100, sometimes just one.

Now I want to count the number of employees from all selected companies and output the result as 1 number (sum of all employees of all selected companies).
How can I do that?

I know it's simple but I can't get it worked out. Thanks.okay, so you query for the number of employees at the selected company.

When you loop through the returned results, simply add them to a variable that starts as 0 instead of loading them into an array.

like:


$query = "SELECT no_of_employees FROM table_of_companies WHERE ". $search_criteria;
$queryexe = mysql_query($query, $connection);

$no_of_employees = 0;

while($row = mysql_fetch_row($queryexe))
{
$no_of_employees = $number_of_employees + $row[0];
}Originally posted by friday
Hey.
I have a sql database which contains more than 100 companies. All these companies have a number of employees.
That number of employees is listed in 1 field per company.

Users can select through a php system what company or group of companies they want to get information from.
SO, sometimes 10 companies are selected, sometimes 100, sometimes just one.

Now I want to count the number of employees from all selected companies and output the result as 1 number (sum of all employees of all selected companies).
How can I do that?

I know it's simple but I can't get it worked out. Thanks.



mysql_query("SELECT count(field) FROM table");thanks!Originally posted by friday
thanks!

sorry for not pointing this out before,but you should use AS with my method if you use it:

mysql_query("SELECT count(field) AS count FROM table");
 
Back
Top