我每周都会获得旅行详细信息并计算他在客户所在地的工作时间。我有一名员工从2016年6月3日到2016年4月30日在外面旅行。该员工当月的可用记录只有一个。< / p>
43 3 International Moho Nether 2016-04-03 14:29:12 2016-04-30 14:29:12 Demo
我想逐周检索记录。所以我尝试了一个查询,它返回某些日期的行,有时却没有。 我只是尝试了这三个查询与一周的日期。
//Last week of april (25th Apr to 1st May)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-25' and date(return_date) <= '2016-05-01'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-25' or '2016-05-01' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-25' and date(travel_date) <= '2016-05-01' ) or (date(return_date) >= '2016-04-25' and date(return_date) <= '2016-05-01')); //1 row(s) returned
//Fourth week (18th Apr to 24th Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-18' and date(return_date) <= '2016-04-24'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-18' or '2016-04-24' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-18' and date(travel_date) <= '2016-04-24' ) or (date(return_date) >= '2016-04-18' and date(return_date) <= '2016-04-24')); //0 row(s) returned
//Third week (11th Apr to 17th Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-11' and date(return_date) <= '2016-04-17'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-11' or '2016-04-17' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-11' and date(travel_date) <= '2016-04-17' ) or (date(return_date) >= '2016-04-11' and date(return_date) <= '2016-04-17')); //0 row(s) returned
//Sec week (4th Apr to 10th Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-04' and date(return_date) <= '2016-04-10'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-04' or '2016-04-10' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-04' and date(travel_date) <= '2016-04-10' ) or (date(return_date) >= '2016-04-04' and date(return_date) <= '2016-04-10')); //0 row(s) returned
//Firs week (28th Mar to 3rd Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-03-28' and date(return_date) <= '2016-04-03'); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-03-28' or '2016-04-03' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-03-28' and date(travel_date) <= '2016-04-03' ) or (date(return_date) >= '2016-03-28' and date(return_date) <= '2016-04-03')); //1 row(s) returned
从上面的测试中,第二个查询适用于所有条件。但是当我在我的PHP文件中使用它时,第二个查询对于日期(4月25日到01月5日)不起作用。
PHP coding:
///////////Employee Outstation(Travel) details/////////////
$employeeTravel = new EmployeeTravelRecord();
$TravelEntryList = $employeeTravel->Find("employee = ? and (date(travel_date) <= ? and ? <= date(return_date))",array($employeeId,$start,$end));
// $TravelEntryList = $employeeTravel->Find("employee = ? and ((travel_date >= ? and travel_date <= ? ) or (return_date >= ? and return_date <= ?))",array($employeeId,$start,$end,$start,$end));
$startdate = $start;
$enddate = $end;
$TravelTime = 0;
foreach($TravelEntryList as $Travelentry){
$TraveldateArr = explode(" ",$Travelentry->travel_date);
$Traveldate = $TraveldateArr[0];
$ReturndateArr = explode(" ",$Travelentry->return_date);
$Returndate = $ReturndateArr[0];
if($startdate >= $Traveldate)
{$firstdate = $startdate;
}
else
$firstdate = $Traveldate;
if($enddate <= $Returndate )
{
$lastdate = $enddate;
}
else
$lastdate = $Returndate;
$holidays = $this->getholidays($firstdate,$lastdate);
$totalhours = $this->getWorkingDays($firstdate,$lastdate,$holidays);
$amount = $totalhours;
error_log("totalhours" . $totalhours);
$TravelTime += $amount;
}
我不知道哪里出错,为了获得一周又一周的记录,只有两个日期(旅行和返回日期)哪一个最好?
我按照第三个查询,通过“离开”和“离开日期”来逐周获取员工的假期。 但它在那里工作得很好。 这就是我为旅行记录选择它的原因。但是这里的第三个查询并不适用于所有情况。
相同的第二个查询返回本周的1行(09月5日 - 5月15日)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-05-09' and '2016-05-15' <= date(return_date));
我怎样才能避免这种情况?
答案 0 :(得分:0)
您正在使用or
条件验证SQL查询(... <= '2016-04-25' or '2016-05-01' <= ...
)中的日期列,但在PHP脚本中,您使用的是AND
而不是or子句({{1 }})。尝试更改条件... <= ? and ? <= ...
(将<= ? **or** ? <=
替换为and
)并检查相同内容。
or