mysql - Query to Find Double-Header -


i have table scheduling of sports. i'd able find when double-header has been scheduled. is, 2 games same team on same day. here's sample table:

id      event           start_date          end_date            homeid awayid 3391    team1 vs. team2 2016-04-27 17:00:00 2016-04-27 18:00:00 3      1 3395    team2 vs. team3 2016-04-27 17:00:00 2016-04-27 18:00:00 5      3 3396    team1 vs. team3 2016-05-04 17:00:00 2016-05-04 18:00:00 5      1 3392    team3 vs. team2 2016-05-04 19:40:00 2016-05-04 20:40:00 3      5 3393    team3 vs. team1 2016-05-11 17:55:00 2016-05-11 18:55:00 1      5 3394    team2 vs. team1 2016-05-18 17:55:00 2016-05-18 18:55:00 1      3 

i'm able find days of double-headers like:

select * events group date_format(start_date,'%j') 

but, need determining team, using teamids set in homeid , awayid columns, has double-header on days. example, need know on 4/27 team id of '3' has double header. on 5/4 it's teamid of '5'.

i'm sure answer simple i'm struggling today.

thanks.

this tricky. 1 way uses union all , group by:

select team, day, count(*) numgames ((select id, homeid team, date(start_date) day        t       ) union       (select id, awayid team, date(start_date)        t       )      ) t group team, day having numgames > 1; 

Comments