I have a mysql table called ‘Booking’ that has a number of fields with 2 being of type ‘datetime’ - ‘start_date’
and ‘end_date’.
I want to be able to query the two fields to see if a query date range falls within any of the records.
For example, I have 1 record in the booking table with a start date and time of, 01/01/08 00:00:00 and an end time of 25/01/08 00:00:00. I want to
build a query to say, give me a count of all records that have a booking between 12/12/07 00:00:00 and 05/01/08. In this example, I would get a count of 1.
Because I do not know how to do this, I am getting the total number of days of the query dates and then looping round each day and testing that date
against the record dates. If I find a match, I quit the loop and do not process the new booking. This is totally not the right way of doing it and positive there must be an easier way.
SELECT * FROM tbl_name
WHERE start_date > ‘12/12/07 00:00:00’
AND end_date < ‘05/01/08 00:00:00’
You would need to be sure to build your date strings according to the locale (date format) used by your MySQL server. For example, today could be 11/6/08 or 6/11/08 depending on whether you’re on this or that side of the pond.