2016-09-14 80 views
1

請參閱以下查詢。將SQL查詢轉換爲codeigniter活動記錄

我想轉換成CodeIgnitor的活動記錄。

SQL查詢:

Select MobNo,Caller,Sum(Tot_In_Call) as Tot_In_Call,Called,Sum(Tot_Out_Call) as Tot_Out_Call 
From (

    SELECT CTE.Caller as MobNo,Incall.Caller,Incall.Tot_In_Call, Null as Called,0 as Tot_Out_Call 
    FROM Call_Details CTE 
    Left Join (
      Select Count(Called) as Tot_In_Call,Caller,Called 
      from call_details 
      Group By Caller,Called 
    ) InCall on (Cte.caller = InCall.Called) 
    Where InCall.Called in($call_array) 
    Group BY CTE.Caller,Incall.Caller,Incall.Tot_In_Call 

    Union All 

    SELECT CTE.Caller as MobNo,Null as Caller,Null as Tot_In_Call, OutCall.Called,OutCall.Tot_Out_Call 
    FROM Call_Details CTE 
    Left Join (
      Select Count(Called) as Tot_Out_Call,caller,Called 
      from call_details 
      group by caller,called 
    ) OutCall on (CTE.caller = OutCall.caller And cte.called = OutCall.called) 
    Where CTE.Caller in($call_array) 
    Group BY CTE.Caller,OutCall.Called,OutCall.Tot_Out_Call 

) As Qry 
WHERE Qry.MobNo in($call_array) 
Group By Qry.MobNo,Qry.Caller,Qry.Called 
order by Qry.MobNo,Qry.Caller,Qry.Called 

我如何轉換這種不使用$this->db->query()

嘗試使用活動記錄進行: -

$this->db->select("MobNo,Caller,Sum(Tot_In_Call) as Tot_In_Call,Called,Sum(Tot_Out_Call) as Tot_Out_Call"); 
$this->db->from("SELECT CTE.Caller as MobNo,Incall.Caller,Incall.Tot_In_Call, Null as Called,0 as Tot_Out_Call 
    FROM Call_Details CTE 
    Left Join (
      Select Count(Called) as Tot_In_Call,Caller,Called 
      from call_details 
      Group By Caller,Called 
    ) InCall on (Cte.caller = InCall.Called) 
    Where InCall.Called in($call_array) 
    Group BY CTE.Caller,Incall.Caller,Incall.Tot_In_Call 

    Union All 

    SELECT CTE.Caller as MobNo,Null as Caller,Null as Tot_In_Call, OutCall.Called,OutCall.Tot_Out_Call 
    FROM Call_Details CTE 
    Left Join (
      Select Count(Called) as Tot_Out_Call,caller,Called 
      from call_details 
      group by caller,called 
    ) OutCall on (CTE.caller = OutCall.caller And cte.called = OutCall.called) 
    Where CTE.Caller in($call_array) 
    Group BY CTE.Caller,OutCall.Called,OutCall.Tot_Out_Call"); 
$this->db->where_in("MobNo",$call_array); 
$this->db->group_by("MobNo,Caller,Called"); 
$this->db->order_by("MobNo,Caller,Called","ASC"); 
$this->db->get(); 

預先感謝您很多。

+0

參考https://www.codeigniter.com/userguide2/database/active_record.html – AkshayP

+0

已經引用,但沒有使用$ this-> db-> query()找到一個方法,這就是爲什麼。 ... –

+0

爲什麼你不想使用$ this-> db-> query() ? – AkshayP

回答

1

如果你真的想這樣做,爲了讓CI逃避你的查詢,你應該分裂您的查詢。

下面我給你舉個例子

$strQuery = $this->db 
    ->select("Count(Called) as Tot_In_Call,Caller,Called", false) 
    ->from("call_details") 
    ->group_by("Caller,Called") 
    ->get_compiled_select(); 

$strQuery = "(".$strQuery.") InCall"; 

$strQuery = $this->db 
    ->select("CTE.Caller as MobNo,Incall.Caller,Incall.Tot_In_Call, Null as Called,0 as Tot_Out_Call", false) 
    ->from("Call_Details CTE") 
    ->join($strQuery,"Cte.caller = InCall.Called","left",false) 
    ->where_in("InCall.Called", $call_array) 
    ->group_by("CTE.Caller,Incall.Caller,Incall.Tot_In_Call") 
    ->get_compiled_select(); 

echo $strQuery; 

上面的查詢給你回的第一內選擇查詢,直到UNION ALL

利用這段代碼,你應該能夠建立你的查詢,並得到你想要什麼

相關問題