我正在寻找从PBX到正式报告的最新活动。我有以下代码,这些代码使我可以很好地了解当今每个代理的所有操作:

 select  e.`event_id`, `event_time` as event_time, e.g_event_id,
     p.`agent_id`,
     `a_name`, `a_number`
     from `c2g`.`events`  as e
     left join `event_parties` as p on e.`g_event_id` = p.`g_event_id`
     Left join `agent` as a on p.agent_id = a.agent_id
     where p.agent_id is not null
     and p.agent_id <> 0
     order by event_time desc;


以下代码应该为我提供每个代理的最新事件:

 select  e.`event_id`, MAX(`event_time`) as event_time, e.g_event_id,
    p.`agent_id`,
    `a_name`, `a_number`
    from `c2g`.`events`  as e
    left join `event_parties` as p on e.`g_event_id` = p.`g_event_id`
    Left join `agent` as a on p.agent_id = a.agent_id
    where p.agent_id is not null
    and p.agent_id <> 0
    GROUP BY a_name
    order by a_name asc;


但是,我实际上并没有得到最新的事件,而得到了最近的事件,但是总是有几个较新的事件被忽略。知道为什么会这样吗?

最佳答案

如果可以尝试这个

select  e.`event_id`, e.`event_time`, e.g_event_id,
p.`agent_id`,
`a_name`, `a_number`
from `c2g`.`events`  as e
left join `event_parties` as p on e.`g_event_id` = p.`g_event_id`
Left join `agent` as a on p.agent_id = a.agent_id
where p.agent_id is not null
and p.agent_id <> 0
GROUP BY a_name
order by e.`event_time` DESC LIMIT 0,1;

关于mysql - 没有选择MySQL Max且具有多个联接的最大值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26260638/

10-14 17:08