获取MySQL数据库中每个团队的最后6条记录

时间:2013-03-30 13:42:08

标签: mysql sql greatest-n-per-group

我有一个包含足球结果的MySQL数据库,并希望仅检索该数据的特定子集。

数据包含一个包含MatchDate,HomeTeam,AwayTeam,HomeGoals,AwayGoals的表

如何检索此数据的子集,其中包含每个团队参与的最后6场比赛?

虽然我可以为一个团队执行此操作,但如何获得包含表中每个团队的最后6个匹配项的单个子集? (我不担心子集可能包含一些重复项。)

3 个答案:

答案 0 :(得分:3)

你的问题不是关于主队或客场球队的问题,所以我假设比赛可以同时存在。

以下查询将获得前六场比赛,无论他们在哪里比赛:

select MatchDate, HomeTeam, AwayTeam, HomeGoals, AwayGoals
from (select m.*,
             (select count(*)
              from matches m2
              where (m.hometeam = m2.hometeam or m.hometeam = m2.awayteam) and
                    m.matchdate <= m2.matchdate
             ) GameCounter
      from matches m
     ) m
where GameCounter <= 6

使用相关子查询来获得匹配。

为了表现,我放弃了相关子查询的想法。这是借用@sgeddes的想法在一个组内计算:

      select m.*
      from (select team, matchdate,
                   @teamCounter:=IF(@prevTeam=Team, @teamCounter+1,1) as teamCounter,
                   @prevTeam:=Team
            from ((select m.hometeam as team, m.*
                   from matches m
                   group by h.hometeam
                  ) union all
                  (select m.awayteam as team, m.*
                   from matches m
                   group by m.awayteam
                  )
                 ) m cross join
                 (select @teamCounter:=0) const
            group by team
            order by team, matchdate desc
           ) m
      where TeamCounter <= 6

答案 1 :(得分:2)

以下是使用user-defined variable

进行此操作的一种方法
select MatchDate, HomeTeam, AwayTeam, HomeGoals, AwayGoals
from (
  select 
    MatchDate, HomeTeam, AwayTeam, HomeGoals, AwayGoals,
    @teamCounter:=IF(@prevHome=HomeTeam,@teamCounter+1,1) teamCounter,
    @prevHome:=HomeTeam
  from yourtable
    join (select @teamCounter:=0) t
  order by HomeTeam, MatchDate desc
  ) t 
where teamCounter <= 6

SQL Fiddle Demo

以下是小提琴的更新:

select team, MatchDate, HomeTeam, AwayTeam, HomeGoals, AwayGoals
from (
  select 
    team, yourtable.MatchDate, HomeTeam, AwayTeam, HomeGoals, AwayGoals,
    @teamCounter:=IF(@prevHome=team,@teamCounter+1,1) teamCounter,
    @prevHome:=team
  from yourtable
    join (
      select distinct matchdate, hometeam team
      from yourtable
      union 
      select distinct matchdate, awayteam
      from yourtable
    ) allgames on yourtable.matchdate = allgames.matchdate
        and (yourtable.hometeam = allgames.team or yourtable.awayteam = allgames.team)
    join (select @teamCounter:=0) t
  order by team, yourtable.MatchDate desc
  ) t 
where teamCounter <= 6
order by team

Updated SQL Fiddle

答案 2 :(得分:0)

我会使用类似的东西:

使用IN(较新的mysql)

select *,HomeTeam AS HOME from myTable
where HomeTeam in
    (select HomeTeam from myTable 
    where Hometeam = HOME 
    order by created_date limit 6)
group by HomeTeam

没有IN(较旧的mysql)

select * from myTable t1
where HomeTeam IN
    (select HomeTeam from myTable t2
    where t2.HomeTeam = t1.HomeTeam
        order by created_date 
        limit 6)
group by t1.HomeTeam

请注意,您应该在这里使用并标记ID。

如果还没有,则此表应具有主键。理想情况下称为ID或[table_name] _id。这将使您能够使用此ID进行子选择和连接。永远不要认为记录无论如何都是独一无二的。使用ID自动递增主键作为一般做法,它将对您有所帮助。