Mysql 返回较宽查询中单个字段分组的最小值

Mysql 返回较宽查询中单个字段分组的最小值,mysql,sql,Mysql,Sql,以下是一个问题: select e.eid, e.event_time as contact_created, cs.name as initial_class, cn.create_time as lead_date from bm_sets.event378 e inner join bm_config.classes cs on cs.id = e.class_id and cs.cid=378 # and cs.name = 'Candidate' lef

以下是一个问题:

select
    e.eid,
    e.event_time as contact_created,
    cs.name as initial_class,
    cn.create_time as lead_date
from bm_sets.event378 e
inner join bm_config.classes cs on cs.id = e.class_id and cs.cid=378 # and cs.name = 'Candidate'
left join bm_sets.conversion378 cn on cn.eid = e.eid and cn.create_time > e.event_time
where e.eid = 283818 
group by eid, contact_created, initial_class, lead_date
此查询的结果如下所示:

eid,    contact_created, initial_class, lead_date
283818  2015-03-07 09:43:42 Hot 
283818  2015-03-10 22:19:47 Candidate   
283818  2015-03-10 22:22:11 Candidate
我需要调整此查询,以便只返回第一条记录,即创建日期为min contact_的记录。但是因为我在其他字段中使用聚合函数,所以我也按初始类进行分组,所以min是基于组合分组的min

每当我使用子查询时,我们的服务器似乎都在挣扎。因此,我尝试使用另一个联接作为筛选器,例如:

inner join bm_sets.event378 e1 on e1.eid = e.eid and e1.event_time < e.event_time
但在运行它之前,我知道这不会起作用,因为eid用户id 283818仍然会返回,因此所有相关数据都会返回

如何将结果限制为仅与事件时间最小值对应的记录


在构造此查询时,我使用where条件283818我自己的用户id进行调试,仅作为健全性检查。当查询就绪时,将不具有此条件,因此结果将适用于许多用户。

如果您只需要top 1,则可以只调用

选择前1名

或者,您需要创建一个函数,返回您创建的最小\u联系人,然后将其与当前创建的\u联系人进行比较

我希望这对你有帮助
谢谢

好的,我知道了。我在groupwise min/max主题的其他so帖子后面使用了一个空的左连接,如so:

将此添加到选择器:

e1.event_time
将此添加到联接:

left join bm_sets.event378 e1 on e1.eid = e.eid and e1.event_time < e.event_time

请用您正在使用的数据库标记您的问题。已将mysql添加到标记中。因为我希望有一个跨sql平台的解决方案,所以我把它漏掉了,但这是第二个好处。@DougFirr-您能添加示例数据和预期的输出吗
and e1.event_time is null