0

I have a mysql table with some date ranges, for example:

start: 2015-09-10 end: 2015-09-10

Now, I have to check if another date range is inside the start-end date, even if only for some days.

  1. 2015-09-02 -> 2015-09-03 is outside the range;
  2. 2015-09-05 -> 2015-09-11 is inside the range;
  3. 2015-09-11 -> 2015-09-12 is inside the range;
  4. ...

I have these query, it works for case 1 and 2:

SELECT * FROM prenotazione WHERE id_stanza=1 AND (start BETWEEN '2015-09-11' AND '2015-09-12' and end BETWEEN '2015-09-11' AND '2015-09-12' )

For the case 3, the query returns no result, even id the given date are inside the range. Can you help me?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
cent89
  • 113
  • 1
  • 9
  • possible duplicate of [Comparing date ranges](http://stackoverflow.com/questions/143552/comparing-date-ranges) – Luca Rainone Sep 02 '15 at 08:43
  • Dude please check the data it is not inside, result is correct between check like as **start<='2015-09-11' AND start<='2015-09-12** according to it it this condition is not satisfying – Satender K Sep 02 '15 at 09:01
  • Dude please check the data it is not inside, result is correct between check like as **start('2015-09-10')<='2015-09-11' AND start('2015-09-10')<='2015-09-12'** according to it it this condition is not satisfying and same for 'end' – Satender K Sep 02 '15 at 09:03
  • how does the 3rd date range lie between '2015-09-10' and '2015-09-10'? – mynawaz Sep 02 '15 at 09:15
  • possible duplicate of [Determine Whether Two Date Ranges Overlap](http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – Salman A Sep 03 '15 at 17:46

1 Answers1

1

use OR to match either start or end

SELECT * FROM prenotazione 
WHERE id_stanza=1 AND
((`start` BETWEEN '2015-09-11' AND '2015-09-12') OR (`end` BETWEEN '2015-09-11' AND '2015-09-12'))
mynawaz
  • 1,599
  • 1
  • 9
  • 16