【发布时间】:2016-03-23 04:49:57
【问题描述】:
我有该员工的旅行记录。我想检查特定月份的他是在外站(在外面旅行)还是在办公室,以计算旅行小时数。只是我们正在维护旅行数据库,因为我们输入了员工姓名,客户地点旅行,旅行日期和返回日期。
一名员工拥有以下数据:
- 出行日期:“2015-08-29”(2015 年 8 月 29 日)
- 返回日期:'2015-11-06'(2015 年 11 月 6 日)
所以在这里,我想在 10 月份检查所有不在办公室的员工。显然这个人应该属于那个类别,但我找不到他。
我也直接在 MySQL workbench 中试了一下,没有得到结果。
我原来的 PHP 代码:
// $req['date_start'] = '2015-10-01'
// $req['date_end'] = '2015-10-31'
$employeeTravel = new EmployeeTravelRecord();
$TravelEntryList = $employeeTravel->Find("(travel_date between ? and ? or return_date between ? and ? )",array($req['date_start'], $req['date_end'],$req['date_start'], $req['date_end']));
$startdate = $req['date_start'];
$enddate = $req['date_end'];
foreach($TravelEntryList as $Travelentry){
$key = $Travelentry->employee;
if($startdate >= $Travelentry->travel_date)
{
$firstdate = $startdate;
}
else
$firstdate = $Travelentry->travel_date;
if($enddate <= $Travelentry->return_date )
{
$lastdate = $enddate;
}
else
$lastdate = $Travelentry->return_date;
$holidays = $this->getholidays($firstdate,$lastdate);
$totalhours = $this->getWorkingDays($firstdate,$lastdate,$holidays); //It returns in total time of outstation in hours excluding company holidays
$amount = $totalhours;
if(isset($TravelTimeArray[$key])){
$TravelTimeArray[$key] += $amount;
}else{
$TravelTimeArray[$key] = $amount;
}
}
但是我的输入数据没有检索到那个特定的员工记录,因为旅行日期和返回日期都不属于我的输入日期。
MySQL 工作台:
SELECT * FROM employeetravelrecords where travel_date between '2015-10-01' and '2015-10-31' or return_date between '2015-10-01' and '2015-10-31';
我只得到以下结果:
+----+----------+---------------+----------------+----------+------------------+------------------+----------------+
| id | employee | type | project | place | travel date | return date | details |
+----+----------+---------------+----------------+----------+------------------+------------------+----------------+
| 13 | 38 | International | PVMTC Training | Hongkong | 11/10/2015 13:33 | 28/11/2015 13:33 | PVMTC Training |
| 14 | 48 | International | PVMT | VIETNAM | 10/10/2015 9:28 | 1/1/2016 9:28 | PETRO |
| 17 | 57 | International | PVMT | Saudi | 10/10/2015 11:39 | 2/1/2016 11:39 | |
+----+----------+---------------+----------------+----------+------------------+------------------+----------------+
此查询未检索到以下记录:
+---+----+---------------+------+-----+-----------------+-----------------+-----------------+
| 7 | 22 | International | MOHO | XYZ | 29/8/2015 18:00 | 6/11/2015 18:00 | FOR DDS review |
+---+----+---------------+------+-----+-----------------+-----------------+-----------------+
【问题讨论】:
-
也包括 employeetravelrecords 架构。
-
您需要测试重叠。您所写的条件要求它实际上在 10 月开始或结束。
travel_date <= ?end and ?start <= return_date -
嗯,
29/8/2015不在'2015-10-01' and '2015-10-31'和6/11/2015之间,不在'2015-10-01' and '2015-10-31'之间。那么它为什么要出现呢?它不符合您的查询条件。 -
"28/11/2015 13:33" 看起来不像是日期数据类型。那里发生了什么?
-
我将数据从mysql复制到excel。所以日期列的格式是这样的......就是这样。