[英]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: 到目前为止,我已经创建了:
It show's a total of all records in the database with startdate and status as given into the SQL SELECT function. 它显示了数据库中所有记录的总数,其起始日期和状态如SQL SELECT函数中给出的那样。 So $totalWorkedTime = 16.68.
因此$ totalWorkedTime = 16.68。 But this is not what I want.
但这不是我想要的。 I want to show for each department the total value.
我想显示每个部门的总价值。
$datestring = '23-10-2017'; $qClosure = 'SELECT * FROM timeRegistration WHERE startdate="'. $datestring .'" && status="3" '; $rClosure = mysqli_query($conn, $qClosure); $totalWorkedTime = 0; while($row = mysqli_fetch_assoc($rClosure)) { $totalWorkedTime += $row['worktime']; } echo $totalWorkedTime;
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; $ totalhoursDepartment5 = 6.84; $totalhoursDepartment4 = 9.84;
$ totalhoursDepartment4 = 9.84;
The function should create more $totalhoursDepartment if is there any other records with department: 3 or 2 (example). 如果部门中有其他记录:3或2,则该函数应创建更多$ totalhoursDepartment。
SQL sum() will do this for you: SQL sum()将为您完成此操作:
SELECT SUM(worktime),department
FROM Depts
GROUP BY department;
Department should be a foreing key to a department table. 部门应该是部门表的重要关键字。
$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' 使用“ sum”和“ in”
http://sql.sh/cours/where/in and http://sql.sh/cours/where/in和
http://sql.sh/fonctions/agregation/sum http://sql.sh/fonctions/agregation/sum
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.