2016-07-07 50 views
0

我有一個假表是這樣的:計算從MySQL DATABSE離開

**employeeId action  type  effective_date** 
    1   leave  paid  2016-01-15 
    2   leave  unpaid  2016-01-16 
    1   return  return  2016-01-17 
    2   return  return  2016-01-17 

我嘗試此查詢:

$query = (select * from table where employeeId = "$empID" && type = "$type"); 

在此之後我被困;其實我想:

where Empid = 1 && action = leave && type = paid orderby effective_date 

$i=0; 
$j=$i+1; 
$leave =0; 
while($row = mysqli_fetch_array($query)){ 

    $leave = $row['effective_date'][$i] - $row['effective_date'][$j]; 
    $i+1; 
    $j+1; 
    $leaves = $leaves+$leave; 
} 

你能幫我解決這個問題嗎?或者有更好的計算假期的方法嗎?

+0

試圖通過在phpMyAdmin運行所需的查詢開始,或任何SQL編輯器,你正在使用。並在您的查詢中進行計算,而不是使用PHP。 –

+0

我知道這不是一個答案,但在成本方面,爲什麼不使用現有的時間管理系統? http://www.kimai.org/ –

回答

0

你必須ORDERBY之間給予空間:

SELECT * FROM `employee` 
    WHERE `employeeId`='1' 
    AND `action` = 'leave' 
    AND `type` = 'paid' 
ORDER BY `effective_date` ASC 

輸出

+------------+--------+------+---------------------------+ 
| employeeId | action | type |   effective_date | 
+------------+--------+------+---------------------------+ 
|   1 | leave | paid | January, 15 2016 00:00:00 | 
+------------+--------+------+---------------------------+ 

小提琴:http://sqlfiddle.com/#!9/5bac3/3

這會給你葉數:

SELECT `employeeId`, COUNT(*) AS `NumLeaves` FROM `employee` 
    WHERE `employeeId`='1' 
    AND `action` = 'leave' 
    AND `type` = 'paid' 
GROUP BY `employeeId` 
ORDER BY `effective_date` ASC 

輸出

+------------+-----------+ 
| employeeId | NumLeaves | 
+------------+-----------+ 
|   1 |   1 | 
+------------+-----------+ 

小提琴:http://sqlfiddle.com/#!9/5bac3/6

+0

葉子數量應該返回effective_date - leave effictive_date - re date =總假期天數 – Raj