0

here's my data structure:

seasons
id  from         to           name
-----------------------------------------
1   2015-11-01   2015-12-15   season1
2   2015-12-16   2015-12-30   season2
3   2015-12-31   2016-01-20   season3

i need a sql query which will return all records between date range 2015-12-10 and 2015-12-20 - which would be record 1 and 2. any ideas?

Fuxi
  • 7,611
  • 25
  • 93
  • 139
  • Look up how BETWEEN works. It's pretty much "where date is between thisdate and thatdate" – durbnpoisn Nov 30 '15 at 21:41
  • Possible duplicate of [How do I query between two dates using MySQL?](http://stackoverflow.com/questions/3822648/how-do-i-query-between-two-dates-using-mysql) – DACrosby Nov 30 '15 at 21:44
  • but isn't the BETWEEN operator just for one date .. i want to compare one range with another range – Fuxi Nov 30 '15 at 21:45
  • It seems you are looking after *overlapping* interval detection. `BETWEEN` operator will not work in this case. – Giorgos Betsos Nov 30 '15 at 21:45

2 Answers2

1

best explanation for date overlap Determine Whether Two Date Ranges Overlap

SELECT *
FROM YourTable
WHERE `from` <= '2015-12-20'
  AND `to` >= '2015-12-10'

Please not use reserved words like from as field names

Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
-1

You should use the BETWEEN operator.

Like here: How do I query between two dates using MySQL?

Edit:

According to your data structure, you could use > and < operators.

SELECT * FROM `seasons` 
WHERE from > '2015-12-10' AND to < '2015-12-20'
Community
  • 1
  • 1
fos.alex
  • 5,317
  • 4
  • 16
  • 18