我正在开发一个在线时间卡系统并遇到了障碍。我需要做的是在每行计算timeIn
和timeOut
,以确定员工每天可以进出几次的日期。我有一个工作查询来查找整天的总数,但不是每个单独的行。我已经包含了数据库表的结构以及我希望看到的一些示例。
+----+------------+----------+---------+------------------------------+-------------+------------+-----------+----------+
| id | employeeID | date | timeIn | jobDescription | equipType | unitNumber | unitHours | timeOut |
+----+------------+----------+---------+------------------------------+-------------+------------+-----------+----------+
| 8 | 1 | 01/15/13 | 7:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 9:30 PM |
| 9 | 1 | 01/15/13 | 10:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 12:00 PM |
| 10 | 1 | 01/16/13 | 7:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 5:30 PM |
| 11 | 1 | 01/17/13 | 7:00 AM | Loaded sand in Jefferson | Excavator | 345 | NULL | 5:30 PM |
| 12 | 1 | 01/18/13 | 8:00 AM | Backfill in Whispering Creek | Skid Loader | 297 | NULL | 5:30 PM |
| 13 | 1 | 01/19/13 | 8:00 AM | Backfill in Whispering Creek | Skid Loader | 297 | NULL | 3:30 PM |
| 1 | 1 | 01/20/13 | 6:00 | | Excavator | 01E | 7238 | 17:00 |
| 2 | 1 | 01/21/13 | 6:00 | Worked in Jefferson | Excavator | 01E | 7238 | 17:00 |
| 3 | 1 | 01/22/13 | 6:00 | Worked in Jefferson | Excavator | 02E | 7238 | 17:30 |
| 4 | 1 | 01/23/13 | 6:00 | Worked in Whispering Creek | Skid Loader | 32SL | 2338 | 18:30 |
| 5 | 1 | 01/24/13 | 8:00 | Worked in Hubbard | Scraper | 54C | 9638 | 11:30 |
| 6 | 1 | 01/25/13 | 8:00 | Worked in Jefferson | Dozer | 4D | 941 | 19:30 |
| 7 | 1 | 01/26/13 | 8:00 | Pushed Snow | Loader | 950H | 342 | 20:30 |
+----+------------+----------+---------+------------------------------+-------------+------------+-----------+----------+
好的,我希望看到的是,例如日期01/15/13
出现两次,其中timeIn
和timeOut
值不同我希望得到一个值,然后说明在行中id为8的时间是2:30,然后再获得另一个值,说id在9的行中有2:00小时。如果可能,我想用一个查询来完成这个。
这是我的查询以获得一整天的时间:
SELECT `employeeID`, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(`timeOut`, `timeIn`)))) AS `totalTime` FROM `timeRecords` WHERE `date` = "01/15/13" AND `employeeID` = 1 GROUP BY `employeeID`;
答案 0 :(得分:1)
如果我理解你,你需要这个吗?
SELECT `employeeID`, TIMEDIFF(`timeOut`, `timeIn`) AS `totalTime` FROM `timeRecords`;
您可以使用PDO query
查询数据库,该数据库将结果集作为关联数组的数组返回。为了获得更好的性能,您可以使用PDO prepare/execute
。
<?php
function getTimeByRow($connection) {
$sql = "SELECT `employeeID`, TIMEDIFF(`timeOut`, `timeIn`) AS `totalTime` FROM `timeRecords`";
foreach ($connection->query($sql) as $row) {
print $row['employeeID'] . "\t";
print $row['totalTime'] . "\n";
}
}
?>
答案 1 :(得分:1)
所以你想做这样的事情:
从timeRecords中选择employeeID,date,TIMEDIFF(超时,timein),其中date = x和employeeID = y