nhatimme
nhatimme

Reputation: 421

PHP store total count in a variable for each specific MySQL column value

Im trying to create a Day Closure for our factory (employees).

What I have created so far:

My goal is as following:

I want that my function stores those values for each department. How can I manage this? An example is:

$totalhoursDepartment5 = 6.84; $totalhoursDepartment4 = 9.84;

The function should create more $totalhoursDepartment if is there any other records with department: 3 or 2 (example).

MySQL DB

Upvotes: 0

Views: 354

Answers (2)

Nic3500
Nic3500

Reputation: 8611

SQL sum() will do this for you:

 SELECT SUM(worktime),department
 FROM Depts
 GROUP BY department;

Department should be a foreing key to a department table.

Upvotes: 1

Sco
Sco

Reputation: 759

    $datestring = '23-10-2017';
    $qClosure = 'SELECT SUM(worktime) AS totalworktime
FROM timeRegistration
Where departement in (select departement
from timeRegistration
WHERE startdate="'. $datestring .'"
and status="3")';
    $rClosure = mysqli_query($conn, $qClosure);

?

Use 'sum' and 'in'

http://sql.sh/cours/where/in and

http://sql.sh/fonctions/agregation/sum

Upvotes: 0

Related Questions