我有一个名为tblprocesstimekeeping的表,下面有以下数据:
假设我有一个日期范围 2019-05-01-2019-05-15
我想做的是在每个empCompID的tblprocesstimekeeper的数据中的给定范围之间插入缺失的日期。
这是我的代码:
$begin = new DateTime('2019-05-01'); // Get Begin Date
$end = new DateTime('2019-05-15'); // Get End Date
$end = $end->modify( '+1 day' ); // Increment +1 Day
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end); // Get Date Range
foreach($daterange as $date){
$dateNumber = $date->format("Y-m-d"); // counter date + 1 day
//Select company access id and date from tblprocesstimekeeping
$query = $this->db->query("SELECT * FROM tblprocesstimekeeping where companyAccessID = '$companyAccessID'");
foreach ($query->result() as $row){
$empCompID = $row->empCompID;
$date = $row->date;
//echo $date .' = '. $empCompID.'<br>';
if ($date == $dateNumber) {
echo $date .' = '. $empCompID.' = GOOD <br>';
} else{
echo $dateNumber .' = '. $empCompID.' = NO DATA <br>';
}
}
}
输出应为:
2019-05-01 = 11007 - NO DATA
2019-05-01 = 20003 - NO DATA
2019-05-02 = 11007 - GOOD
2019-05-02 = 20003 - GOOD
2019-05-03 = 11007 - GOOD
2019-05-03 = 20003 - GOOD
2019-05-04 = 11007 - GOOD
2019-05-04 = 20003 - GOOD
2019-05-05 = 11007 - NO DATA
2019-05-05 = 20003 - NO DATA
2019-05-06 = 11007 - NO DATA
2019-05-06 = 20003 - GOOD
2019-05-07 = 11007 - GOOD
2019-05-07 = 20003 - GOOD
2019-05-08 = 11007 - GOOD
2019-05-08 = 20003 - GOOD
2019-05-09 = 11007 - GOOD
2019-05-09 = 20003 - GOOD
2019-05-10 = 11007 - GOOD
2019-05-10 = 20003 - GOOD
2019-05-11 = 11007 - GOOD
2019-05-11 = 20003 - GOOD
2019-05-12 = 11007 - NO DATA
2019-05-12 = 20003 - NO DATA
2019-05-13 = 11007 - NO DATA
2019-05-13 = 20003 - NO DATA
2019-05-14 = 11007 - GOOD
2019-05-14 = 20003 - GOOD
2019-05-15 = 11007 - GOOD
2019-05-15 = 20003 - GOOD
但是我却得到了以下输出:
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-01 = 11007 = NO DATA
2019-05-01 = 20003 = NO DATA
2019-05-02 = 11007 = GOOD
2019-05-02 = 20003 = GOOD
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-02 = 11007 = NO DATA
2019-05-02 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = GOOD
2019-05-03 = 20003 = GOOD
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-03 = 11007 = NO DATA
2019-05-03 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = GOOD
2019-05-04 = 20003 = GOOD
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-04 = 11007 = NO DATA
2019-05-04 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-05 = 11007 = NO DATA
2019-05-05 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 20003 = GOOD
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-06 = 11007 = NO DATA
2019-05-06 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = GOOD
2019-05-07 = 20003 = GOOD
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-07 = 11007 = NO DATA
2019-05-07 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = GOOD
2019-05-08 = 20003 = GOOD
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-08 = 11007 = NO DATA
2019-05-08 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = GOOD
2019-05-09 = 20003 = GOOD
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-09 = 11007 = NO DATA
2019-05-09 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = GOOD
2019-05-10 = 20003 = GOOD
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-10 = 11007 = NO DATA
2019-05-10 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = GOOD
2019-05-11 = 20003 = GOOD
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-11 = 11007 = NO DATA
2019-05-11 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-12 = 11007 = NO DATA
2019-05-12 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-13 = 11007 = NO DATA
2019-05-13 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-14 = 11007 = GOOD
2019-05-14 = 20003 = GOOD
2019-05-14 = 11007 = NO DATA
2019-05-14 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = NO DATA
2019-05-15 = 20003 = NO DATA
2019-05-15 = 11007 = GOOD
2019-05-15 = 20003 = GOOD
编辑:
如果我将查询更改为此
SELECT * FROM tblprocesstimekeeping where companyAccessID = '$companyAccessID' and date='$dateNumber'
它仅显示以下输出:
2019-05-02 = 11007 = GOOD
2019-05-02 = 20003 = GOOD
2019-05-03 = 11007 = GOOD
2019-05-03 = 20003 = GOOD
2019-05-04 = 11007 = GOOD
2019-05-04 = 20003 = GOOD
2019-05-06 = 20003 = GOOD
2019-05-07 = 11007 = GOOD
2019-05-07 = 20003 = GOOD
2019-05-08 = 11007 = GOOD
2019-05-08 = 20003 = GOOD
2019-05-09 = 11007 = GOOD
2019-05-09 = 20003 = GOOD
2019-05-10 = 11007 = GOOD
2019-05-10 = 20003 = GOOD
2019-05-11 = 11007 = GOOD
2019-05-11 = 20003 = GOOD
2019-05-14 = 11007 = GOOD
2019-05-14 = 20003 = GOOD
2019-05-15 = 11007 = GOOD
2019-05-15 = 20003 = GOOD
缺少的日期仍然没有显示。.
答案 0 :(得分:0)
这是您的问题:您正在表中创建不存在的行。对于不在表中的日期,您将创建一个指示表中没有行的行。
这使此问题在您采用的方法上有些棘手。您可以构造一个复杂的MYSQL查询来处理此问题,也可以在PHP中对其进行处理。
更好的方法是每天遍历所有用户。
// List of all users to find
$users = ['11007', '20003'];
foreach ($daterange as $date) {
// Get date in database format
$dateNumber = $date->format("Y-m-d"); // counter date + 1 day
// Go through all users
foreach ($users as $user) {
// Find all accesses for that user and day
$query = $this->db->query("SELECT * FROM tblprocesstimekeeping WHERE companyAccessID = '$companyAccessID' AND empCompID = '$user' AND date = '$dateNumber'");
// If there is a row, then paste it, otherwise past NO DATA
if ($query->num_rows()) {
echo $dateNumber.' = '.$user->id.' = GOOD <br>';
} else {
echo $dateNumber.' = '.$user->id.' = NO DATA <br>';
}
}
}
答案 1 :(得分:0)
您的算法不正确。 由于您没有按日期过滤表数据,因此对于日期范围内的每个日期,您都选择了tblprocesstimekeeping表中的所有行,并为所有行打印结果(这就是每个日期有21行的原因)。
我不熟悉php,因此我不会在此处发布代码,但这是我的建议:
首先,您需要知道员工的ID 您可以使用以下查询从数据库中获取它们:
“select distinct empCompId from FROM tblprocesstimekeeping where companyAccessID = '$companyAccessID'"
并为它们保存某种软件(也许在数组中)
然后,要完成您想要的操作,您将需要执行两次cicle
// first iterate by date
foreach($daterange as $date)
// then, for each date iterate by employee
foreach($employees as $empId){
// check existance on record
If (exists on data base)
Echo ‘good’
else
echo ‘nodata’
对于“数据库中是否存在支票”,您只需更改查询即可按日期和日期过滤表数据
$query = $this->db->query("SELECT * FROM tblprocesstimekeeping where companyAccessID = '$companyAccessID' and date='$dateNumber'
");
但是您应该考虑到这一点:
您连续调用数据库的次数与日期范围内的日期一样。
如果可能的话,请考虑一种算法,该算法仅一次调用datadate ,并返回日期范围开头到结尾的所有数据(在foreach $ daterange子句之前) 然后,您可以对数据进行简单过滤,以检查代码中特定日期的存在。
如果涉及的记录数量不是很高(这里只有21条记录),那么此解决方案的性能将会好得多
答案 2 :(得分:0)
我考虑过您的问题,并且我认为有更好的解决方案。
创建一个存储过程(SP)来为您完成所有工作。
这将创建一个SP,该SP的参数将传递公司标识符,要显示的日期范围的开始日期和结束日期,并返回所需的结果(日期,employeeid和返回0的标记“ found”没有记录时,则没有tblprocesstimekeeper表;如果存在,则为1。
DELIMITER //
DROP PROCEDURE IF EXISTS myListing //
CREATE PROCEDURE myListing(companyId int, startdate date, endDate date)
BEGIN
WITH RECURSIVE days(day) AS
(
select startdate UNION ALL
select DATE_ADD(day,INTERVAL 1 DAY) FROM days WHERE day<= endDate
)
select days.day as date, Employees.Id as empCompId, case when Time.date IS NULL THEN 0 else 1 end as found
FROM days
cross join
(select distinct empCompId as Id from tblprocesstimekeeping where companyAccessID = companyId and date >= startdate and date <= endDate) AS Employees
LEFT JOIN
(select * from tblprocesstimekeeping where companyAccessID = companyId and date >= startdate and date <= endDate) AS Time on days.day = Time.date and Employees.Id = Time.empCompId
order by days.day, Employees.Id;
END
//
DELIMITER ;
然后,您只需调用它即可传递所需的参数,然后遍历结果并根据找到的标志打印数据。
set @startdate = '2019-05-01';
set @endDate = '2019-05-15';
set @companyId = 1;
CALL myListing(@companyId, @startdate, @enddate);
Se DBfiddler here