2015-03-03 41 views
-3
Select SUM(getWorkingday(Dates.DateFrom,Dates.DateTo,'work_days')) as UnpaidLeave 
from (Select convert(case when (YEAR(`DateFrom`)=paramyear and YEAR(`DateTo`)=paramyear and Month(`DateFrom`)<paramonth and Month(`DateTo`)= paramonth) then STR_TO_DATE(concat(Year(`DateFrom`),'-',Month(`DateFrom`)+1,'-','01') ,'%Y-%m-%d') Else `DateFrom` end , date) as DateFrom 
     , convert(case when (YEAR(`DateFrom`)=paramyear and YEAR(`DateTo`)=paramyear and Month(`DateFrom`)= paramonth and Month(`DateTo`)>paramonth) then STR_TO_DATE(concat(Year(`DateTo`),'-',Month(`DateTo`)-1,'-',DAY(LAST_DAY(`DateFrom`))) ,'%Y-%m-%d') Else `DateTo` end , date) as DateTo 
    from leaveenjoyed where `EmpId`=paramempid and `LeaveTypeId` in (-1) and (Year(`DateFrom`)= paramyear or Year(`DateTo`)= paramyear) AND (Month(`DateFrom`)= paramonth or Month(`DateTo`)= paramonth)) as Dates 
+2

爲什麼我們做你的家庭作業?你看看CI的成長嗎?你有嘗試過什麼嗎? – 2015-03-03 07:14:36

+1

[這是文檔](https://ellislab.com/codeIgniter/user-guide/database/active_record.html) – 2015-03-03 07:15:24

回答

0

嘗試這樣的,這是更好地查詢

$this->db->query('Select SUM(getWorkingday(Dates.DateFrom,Dates.DateTo,'work_days')) as UnpaidLeave 
from (Select convert(case when (YEAR(DateFrom)=paramyear and YEAR(DateTo)=paramyear and Month(DateFrom)paramonth) then STR_TO_DATE(concat(Year(DateTo),'-',Month(DateTo)-1,'-',DAY(LAST_DAY(DateFrom))) ,'%Y-%m-%d') Else DateTo end , date) as DateTo from leaveenjoyed where EmpId=paramempid and LeaveTypeId in (-1) and (Year(DateFrom)= paramyear or Year(DateTo)= paramyear) AND (Month(DateFrom)= paramonth or Month(DateTo)= paramonth)) as Dates 
') 
+0

您的查詢生成錯誤。 – 2015-03-03 07:18:28

+0

使用雙引號 - (「-query-」) – 2015-03-03 07:27:14

+0

首先檢查您的查詢,然後放入$ this-> db-> query('/ * Inside your query * /'); – 2015-03-03 07:31:04