gpt4 book ai didi

php - SQL 查询转换为 codeigniter 事件记录

转载 作者:行者123 更新时间:2023-11-28 23:25:39 25 4
gpt4 key购买 nike

请看下面的查询。

我想转换成 CodeIgnitor Active Records。

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();

非常感谢您。

最佳答案

如果您真的想这样做以使 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

使用这段代码,您应该能够构建您的查询并获得您想要的内容

关于php - SQL 查询转换为 codeigniter 事件记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com