JohnathanKong
JohnathanKong

Reputation: 1307

MySQL Select between an unknown number of date intervals

I currently have two tables, one is called games, the other called rounds. Here is the structure to help out:

rounds

+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| id          | int(11)  | NO   | PRI | NULL    | auto_increment |
| schedule_id | int(11)  | NO   | MUL | NULL    |                |
| open_date   | datetime | NO   |     | NULL    |                |
| close_date  | datetime | NO   |     | NULL    |                |
| start_date  | datetime | NO   |     | NULL    |                |
| end_date    | datetime | NO   |     | NULL    |                |
+-------------+----------+------+-----+---------+----------------+

games

+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| id          | int(11)  | NO   | PRI | NULL    | auto_increment |
| schedule_id | int(11)  | NO   | MUL | NULL    |                |
| team_a      | int(11)  | NO   | MUL | NULL    |                |
| team_b      | int(11)  | NO   | MUL | NULL    |                |
| winner      | int(11)  | YES  | MUL | NULL    |                |
| date        | datetime | NO   |     | NULL    |                |
+-------------+----------+------+-----+---------+----------------+

Now the rounds table is a bunch of rounds with specified date ranges, and games are set within the round date range (but not always). Anyways, What I want to do is select all games between multiple round intervals. So if I have three round intervals,

round 1 - nov 13 to nov 15
round 2 - nov 20 to nov 25
round 3 - dec 1 to nov 10

how do I only select the games that are between those date periods without doing multiple SQL statements. Essentially I want something like this in the end:

SELECT * 
FROM games 
WHERE date BETWEEN round1.start_date AND round1.end_date OR
      date BETWEEN round2.start_date AND round2.end_date OR
      date BETWEEN round3.start_date AND round3.end_date

but I don't know how many rounds I will have. I also cannot use a max a min because there might be rounds between those intervals that I do not want. Right now my only solution is that I do one select for all the rounds, then do a select for each interval through PHP. I am hoping that there might be a better, more efficient way to accomplish this.

Hopefully this made sense and thanks!

Update:

After posting this question, I had an idea of a query like this:

SELECT * 
FROM games INNER JOIN 
     rounds ON games.`date` BETWEEN rounds.start_date AND rounds.end_date

does something like that make sense? It seems to work, but I've never joined like that, so I don't know what type of effects I might encounter.

Upvotes: 3

Views: 729

Answers (1)

superfro
superfro

Reputation: 3302

You were pretty close with your update...

select `games`.* from games join rounds on (`games`.`date` < `rounds`.`end_date` AND `games`.`date` > `rounds`.`start_date`);

Upvotes: 1

Related Questions