2016-03-23 36 views
0

兩個日期我有員工的旅行史之間。我要檢查,對於特定的月份,無論他是在分站(旅行外),或在辦公室,要計算的旅遊小時數。只要我們保持行程數據庫中,我們與旅行日期和返回日期前往客戶所輸入的員工姓名。如何檢查給定的日期期間在MySQL和PHP

一個員工有以下數據:

  • 旅行日期: '2015年8月29日'(2015年8月29日)
  • 返回的日期: '2015年11月6日'(2015年11月6日)

所以在這裏,我想在十月份檢查所有不在辦公室的員工。顯然這個傢伙應該進入這個類別,但我無法得到他。
我也試過直接在MySQL工作臺,但我沒有得到的結果。

我原來的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 | 
+---+----+---------------+------+-----+-----------------+-----------------+-----------------+ 
+0

包括** ** employeetravelrecords模式也是如此。 –

+1

您需要測試重疊。你寫的條件要求它在10月份開始或結束。 'travel_date <=?end and?start <= return_date' – shawnt00

+2

那麼,2015/9/15不在2015/10-01和2015-10-31之間以及2015年6月11日之前'不在'2015-10-01'和'2015-10-31'之間。那爲什麼它會出現?它不符合您的查詢條件。 「 – Sean

回答

-1

您可以使用用於在兩個日期之間獲取數據。這是一個工作示例。

這裏是我的表結構:

表用戶

user_id user_name created_date   modified_date 
1   lalji nakum 2016-01-28 17:07:06  2016-03-31 00:00:00 
2   admin  2016-01-28 17:25:38  2016-02-29 00:00:00 

這裏是我的MySQL查詢:

查詢

SELECT * FROM `user` WHERE created_date between '2015-12-01' and '2016-12-31' or modified_date between '2015-12-01' and '2016-12-31' 

結果的上述查詢:

結果

user_id  user_name created_date   modified_date 
1   lalji nakum 2016-01-28 17:07:06  2016-03-31 00:00:00 
2   admin  2016-01-28 17:25:38  2016-02-29 00:00:00 
+0

實際上,您可以在我的問題中看到我使用了與您相同的查詢。此外,對於您的數據,您是否嘗試在'2016-02-01'和'2016-02-28'之間進行拍攝,因爲我嘗試使用您的數據,但無法使用。請檢查您的身邊。 – Anu

+0

表格中沒有您提供日期的數據。所以它會返回空結果。 –

-1

你想找到所有這些人誰不提供完整的時間段T1 T2 &(和T2> T1)之間說。您使用只會給用戶,其起始日期和返回日期指定的間隔時這不是必需的輸出之間都位於查詢。

因此,爲了得到你需要檢查誰已經開始在T1或之前,他們的旅程的所有員工和返回日期或之後,T2所需的輸出(從而爲完整的區間[T1,T2]不可用)。所以,你可以使用查詢:

SELECT * FROM employeetravelrecords where travel_date <= '2015-10-01' and return_date >= '2015-10-31'; 

如果你希望員工誰,甚至部分是不是給定時間之間可用的,那麼我們需要的員工誰T1前,開始了他們的旅行和具有任何返回日期晚於T1(從而使他們ATLEAST無法爲給定的時間間隔的一部分):

SELECT * FROM employeetravelrecords where travel_date <= '2015-10-01' and return_date > '2015-10-01'; 
0
SELECT * FROM employeetravelrecords 
WHERE 
     return_date >= '2015-10-01' /* start parameter */ 
    and travel_date <= '2015-10-31' /* end parameter */ 

的邏輯似乎有點令人費解起初,我甚至已經重新排序比較,從我上面的原始註釋一點。想想這樣:你需要在之後返回範圍的開始並且在之前離開以便範圍的結束以便具有重疊。

對於更詳細的解釋和討論你可能會發現此頁面有用:TestIfDateRangesOverlap

+0

我懷疑其中一個不應該是'='。 – Strawberry

0
SELECT '2015-08-29' < '2015-10-31' AND '2015-11-06' >= '2015-10-01' on_leave; 
+----------+ 
| on_leave | 
+----------+ 
|  1 | 
+----------+ 
相關問題