我有一张运动时间表。我希望能够找到何时安排双标头。也就是说,同一天同一支队伍的两场比赛。这是一个示例表:
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
我能够找到双标题的日期,例如:
SELECT *
FROM events
GROUP BY DATE_FORMAT(start_date,'%j')
但是,我需要帮助的是确定哪个团队使用homeid和awayid列中设置的teamid,在那些日子里有双头。例如,我需要在4月27日知道id为'3'的团队有双头。在5/4它的队友为'5'。
我确信答案很简单,但我今天正在努力解决这个问题。
感谢。
答案 0 :(得分:1)
这很棘手。一种方法使用union all
和group by
:
select team, day, count(*) as numgames
from ((select id, homeid as team, date(start_date) as day
from t
) union all
(select id, awayid as team, date(start_date)
from t
)
) t
group by team, day
having numgames > 1;