如何在codeigniter模型文件中编写下面的mysql查询

时间:2015-03-03 07:12:33

标签: codeigniter

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

1 个答案:

答案 0 :(得分: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
')