3

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

数据由一张表组成,其中包含 MatchDate、HomeTeam、AwayTeam、HomeGoals、AwayGoals

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

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

4

3 回答 3

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
于 2013-03-30T15:06:09.340 回答
2

这是使用 a 的一种方法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 的更新:

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

更新的 SQL Fiddle

于 2013-03-30T14:09:46.533 回答
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 自动递增主键作为一般做法,它将对您有很大帮助。

于 2013-03-30T13:53:02.283 回答