如何使用MySQL按团队和按结果检查条纹?

时间:2019-02-17 19:56:32

标签: mysql

我创建了一个视图,其中包括比赛ID,球队,结果(使用“ W”,“ D”,“ L”)和日期。 例如:

'32750', 'Team 1', 'D', '2019-02-16 21:30:00'
'32750', 'Team 2', 'D', '2019-02-16 21:30:00'
'32748', 'Team 3', 'L', '2019-02-16 19:20:00'
'32748', 'Team 4', 'W', '2019-02-16 19:20:00'

我已经调整了在这里找到的查询,该查询使用该表计算了条纹,并向我显示了队伍,条纹的开始日期,结束日期,条纹数以及该条纹中包含的匹配项。

'Team 1', '1992-07-05', '1992-09-06', '5', '9522,9142,9161,9167,9180'

我的问题是我必须在where子句中包含团队名称,否则查询显示不正确的结果。这是我用于获胜条纹的查询

select 
   team_name,
   min(match_date) as start_date,
   max(match_date) as end_date,
   count(match_date) as streak,
   group_concat(idmatch) as gameid_list
from
( 
  select *,      
    IF(
        result = "W"
        and 
        @result = "W", 
           @gn, @gn := @gn + 1)                
    as group_number,

    @match_date as old_date, @idmatch as old_gameid, 
    @result as old_rersult,

    @match_date := match_date, @idmatch := idmatch, 
    @result := result    
  from my_football_database.`Resultados Globales WDL`
  cross join 
  (
    select 
      @match_date := CAST(null as date) as xa,
      @idmatch := null + 0 as xb,
      @result := null + 0 as xc, @gn := 0
  ) x where team_name = "Team 1"
  order by match_date
) as y
group by group_number
order by streak desc;

如您所见,我必须在where子句中指定球队的名称,因此,如果要获得历史上排名前3位的连胜纪录,我必须为每个球队运行此查询150次,并且然后手动比较。

我该如何修改以获得完整的按团队分组的条纹清单?

0 个答案:

没有答案