我正在尝试为我们的工厂(员工)创建一天封闭。
到目前为止我创造了什么:
它显示数据库中所有记录的总数,其中startdate和status在SQL SELECT函数中给出。所以$ totalWorkedTime = 16.68。但这不是我想要的。我想为每个部门显示总价值。
$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;
我的目标如下:
我希望我的函数为每个部门存储这些值。我该怎么办呢?一个例子是:
$ totalhoursDepartment5 = 6.84; $ totalhoursDepartment4 = 9.84;
如果有任何其他记录包含部门,该函数应该创建更多$ totalhoursDepartment:3或2(示例)。
答案 0 :(得分:1)
SQL sum()将为您执行此操作:
SELECT SUM(worktime),department
FROM Depts
GROUP BY department;
部门应该成为部门表的关键。
答案 1 :(得分:0)
$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);
使用'sum'和'in'