[英]Get data by comparing two dates in mysql
我每周获取一次旅行详细信息,并计算出他在客户所在地所花费的工作时间。下面有一位员工从2016年4月3日到2016年4月30日外出旅行,该月该员工的可用记录仅为一个。
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日至5月1日)仍然无效。
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;
}
我不知道错误在哪里,为了仅通过两个日期(旅行日期和返回日期)来获得每周记录,哪一个最好?
我跟踪了第三个查询,以获取雇员离职日期和离职日期来逐周获取雇员离职。 但这在那很好。 这就是为什么我选择它作为旅行记录的原因。 但是,这里第3条查询不适用于所有情况。
同一第二查询在一周中返回1行(5月9日-5月15日)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-05-09' and '2016-05-15' <= date(return_date));
我如何避免这种情况?
您正在使用or
条件来验证SQL查询中的日期列( ... <= '2016-04-25' or '2016-05-01' <= ...
),但是在PHP脚本中,您正在使用AND
而不是or子句( ... <= ? and ? <= ...
)。 尝试更改条件<= ? **or** ? <=
<= ? **or** ? <=
<= ? **or** ? <=
取代and
用or
),并检查是相同的。
TravelEntryList = $employeeTravel->Find("employee = ?
and (date(travel_date) <= ? or ? <= date(return_date))",array($employeeId,$start,$end));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.