i have this mysql table with following records.
table name : gce_arena_surface_booking
+----+---------------------+---------------------+------------------+
| id | start_datetime | end_datetime | arena_surface_id |
+----+---------------------+---------------------+------------------+
| 1 | 2012-11-22 02:30:00 | 2012-11-22 05:00:00 | 2 |
| 2 | 2012-11-22 00:00:00 | 2012-11-22 02:30:00 | 2 |
| 3 | 2012-11-22 05:00:00 | 2012-11-22 08:00:00 | 2 |
| 4 | 2012-11-22 08:00:00 | 2012-11-22 11:00:00 | 2 |
| 5 | 2012-11-22 11:00:00 | 2012-11-22 17:00:00 | 2 |
| 6 | 2012-11-22 17:30:00 | 2012-11-22 22:00:00 | 2 |
| 7 | 2012-11-23 02:00:00 | 2012-11-23 18:00:00 | 2 |
+----+---------------------+---------------------+------------------+
i need to know by given date time, if any booking already exist, i am using this query to check if any booking exist for given start_datetime, and end_datetime
SELECT
id, start_datetime, end_datetime, arena_surface_id
FROM
gce_arena_surface_booking
WHERE (
(start_datetime BETWEEN '2012-11-23 2:00' AND '2012-11-23 6:00') OR
(end_datetime BETWEEN '2012-11-23 2:00' AND '2012-11-23 6:00')
)
AND arena_surface_id = 2
the above query is giving me problems for example if i have the value changed from :
'2012-11-23 2:00' AND '2012-11-23 6:00'
to
'2012-11-23 2:30' AND '2012-11-23 6:00'
note that i have changed the value from 2:00 to 2:30, it does not return me any result if i do that, whereas with reference to above records i expect it to return me a row with id 7 since i have the time slot booked in between 2:00 and and 18:00, and hence any given value that matches time between 02:00 and 18:00 should be returning a row.
what would be the proper mysql query to deal with my problem?
here is the link to sqlfiddle if you want to play around with records and schema.
The bookings that starts when or after your new booking ends, or that ends when or before your new booking starts are no problem.
Thus the bookings you need to find are the ones that both starts before your new booking ends, and ends after your new booking starts.
Your query shoud then be