简体   繁体   English

PHP将总数存储在每个特定MySQL列值的变量中

[英]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: 我的目标如下:

  • A function which stores for the different departments (1, 2, 3, 4, 5, 6) a total count of worked hours. 为不同部门(1、2、3、4、5、6)存储总工作时间的功能。 In my MySQL Database I have a column called: worktime. 在我的MySQL数据库中,我有一列叫做:工作时间。 In this column I have different values like: 3.42, 3.42, 4.92, 4.92 (these are hours). 在此列中,我具有不同的值,例如:3.42、3.42、4.92、4.92(这些是小时)。 So I have 4 records right. 所以我有4条记录。 Now these records have different departments which stored in the column: department. 现在,这些记录具有存储在列中的不同部门:部门。

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。

MySQL数据库

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM