0

我正在创建一个仪表板来让我了解呼叫代理状态的最新信息。代理将在日志中有多个记录。我需要从代理日志中提取最新状态。我发现的唯一方法是查询代理表以提取今天进行状态更改的代理,然后查询代理日志表以提取最新状态。

有没有办法结合这两个查询。?这是我的查询

$sql_get_agents = "SELECT id FROM agent WHERE lastchange LIKE '{$today}%'";
            if($dta = mysql_query($sql_get_agents)){

                while($agent = mysql_fetch_assoc($dta)){
                $curr_agent[] = $agent;

                }

                foreach($curr_agent as $agents_online){
                    $get_status_sql = "SELECT a.firstname,a.lastname,al.agentid,al.agent_statusid,s.id as statusid,s.status,MAX(al.datetime) as datetime FROM agent_log al
                    INNER JOIN agent a ON al.agentid = a.id
                    INNER JOIN agent_status s ON a.agent_statusid = s.id
                    WHERE al.agentid = '{$agents_online['id']}'";
                    if($dta2 = mysql_query($get_status_sql)){                               
                        while($agent_status = mysql_fetch_assoc($dta2)){
                        $curr_status[] = $agent_status;                         
                        }
                    }

                }//end for each

                return $curr_status;
            }//end if
4

3 回答 3

0

你很接近你所拥有的。这将摆脱同时进行查询或循环查询的需要。

编辑:添加示例代码以循环遍历结果。

edit2:更改查询。

$query = "SELECT 
   a.firstname,
   a.lastname,
   al.agentid,
   al.agent_statusid,
   s.id as statusid,
   s.status,
   MAX(al.datetime) as datetime 
FROM agent a
   LEFT JOIN agent_log al ON al.agentid = a.id
   LEFT JOIN agent_status s ON a.agent_statusid = s.id
WHERE a.lastchange LIKE '{$today}%'";

$status = array();
$results = mysql_query( $query );
while( $agent = mysql_fetch_assoc( $results ) )
    $status[] = $agent;

print_r( $status );
于 2013-06-26T15:52:09.033 回答
0

为什么不将 2 个查询合并为一个WHERE lastchange LIKE '{$today}%',在第二个查询中添加条件?

于 2013-06-26T15:48:39.813 回答
0

使用 IN 子句应该有效:

"SELECT a.firstname,a.lastname,al.agentid,al.agent_statusid,s.id as statusid,s.status,MAX(al.datetime) as datetime FROM agent_log al
                    INNER JOIN agent a ON al.agentid = a.id
                    INNER JOIN agent_status s ON a.agent_statusid = s.id
                    WHERE al.agentid IN (SELECT id FROM agent WHERE lastchange LIKE '{$today}%');
于 2013-06-26T15:49:08.743 回答