Mysql:获取每个组的第二个最早日期

Mysql: Getting the 2nd earliest date for each group

期望的结果: 我想获得每个组的第二个最早日期。

当前: 我正在获取每个组的最早日期

查询:

select * from
(select * from profile_match
where match_available_on is not null
order by profile_id asc, match_available_on asc)
as P1
group by P1.profile_id

示例:Link

从例子中,我想得到:

而且我希望查询没有使用特定的 profile_id(1 或 2)进行硬编码,因为我的实际数据有很多 profile_id。谢谢大家!

此查询将为您提供所需的结果:

select t.profile_id, min(t.match_available_on )
  from profile_match t
    inner join (
      select profile_id, min(match_available_on) match_available_on
        from profile_match
        group by profile_id
    ) q
    on t.profile_id = q.profile_id
      and t.match_available_on > q.match_available_on
  group by t.profile_id;

通过找到最小值match_available_on,然后我们可以对其进行范围连接,并选择最小连接值。这不会有惊人的性能,但它会完成工作。

updated your demo here.

对于只有一个可用日期的配置文件,它不会 return 一行,因为没有 'second earliest'。