0

I have an issue with my code. I have 2 tables. First employee_id:

|Employee id|

1

2

3

And the second table called employee_times:

|Employee_id|Hours_dev|hours_pm|

|1|2|3|

|1|3|4|

|2|3|3|

What I am trying to do is to calculate the total time that each employee has worked (hours_dev+hours_pm). For example employee_id 1 has worked 12 hours

So far I have tried to retrieve all the employee_id from the first table and use a for loop to go through the employee_times in an SQL statement (SEE CODE BELOW). However the code does not work as it prints 0 for both employee_id and total_hours.

I am using MYSQL on a localhost server.

$sql = "SELECT employee_id FROM employee"; $result = mysql_query($sql); while($row = mysql_fetch_array) { $employee_id = $row['employee_id']; }

              $employee_id_length = sizeof($employee_id);

              for($i = 0; $i < $employee_id_length; $i++)
              {
                 $sql4 = "SELECT employee_id, hours_dev, hours_pm FROM employee_times WHERE employee_id= '$employee_id[$i]'";
                        $result =  mysql_query($sql4);
                          while($info = mysql_fetch_array($result));
                        {
                            $employee_id = $info['employee_id'];
                            $hours_dev=$info['hours_dev'];
                            $hours_pm=$info['hours_pm'];
                            $total_hours = ($total_hours + $hours_dev + $hours_pm );

                        }

                        //print "$employee_id worked for $total_hours";
              }

Any help is much appreciated.

3
  • 3
    I'm sorry to ask a silly question, but why don't you do it in sql directly? Commented Mar 7, 2013 at 18:41
  • I agree with @Sebas it will be a lot quicker as well. Commented Mar 7, 2013 at 18:41
  • Is it possible to do it directly from sql? I want to find the sum of all the hours an employee has worked for each employee. e.g. you should end up with employee_id 1 has worked 6 hours, employee_id 2 has worked for 5 hours etc. I cannot think of another way of calculating the time for each employee in the table without some sort of iterator. I have tried SELECT employee_id, sum(hour_dev+hours_pm) FROM employee_times GROUP by employee_id but it doesn't work. Commented Mar 7, 2013 at 18:46

4 Answers 4

1

you can get sum directly

 select employee_id, sum(hours_dev)+ sum(hours_pm) as total 
from employee_times WHERE employee_id= '1'
group by employee_id

refer this Fiddle Demo

Sign up to request clarification or add additional context in comments.

4 Comments

does this deal with multiple rows of employee_id, hours_dev and hours_pm?
how would I get it so the result would show both employee 1 has worked 12 hours and employee 2 has worked 6 hours? This is the bit where Im having the most trouble and hence the for loop
i have modify the fiddle demo. check that
The modified version of the fiddle demo is exactly what Im looking for. Thanks for the help
1

this should get the data you need

SELECT
  hours_dev,
  hours_pm,
  sum(hours_dev) + sum(hours_pm) as total_hours
FROM
  employee_times
WHERE
  employee_id = 123
GROUP BY
  employee_id

Comments

1

Take a look at aggregate functions: http://www.w3schools.com/sql/sql_functions.asp http://www.w3schools.com/sql/sql_func_sum.asp

Comments

0

This SQL query should pull the info much quicker than by script;

SELECT Employee_id, SUM(Hours_dev), SUM(Hours_pm), SUM(Hours_dev + Hours_pm) 
FROM employee_times 
GROUP BY Employee_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.