sql - MYSQL query on DateTime, find existing bookings -
I have a table that car booked for parking spaces. The minimum booking time is 30 minutes and the maximum booking period can be 1 full day (i.e. a booking never goes on several days). Booking should not overlap, that is, only one car per place at one time, so I first check to see if the booking is available before entering a new booking.
ID purchased datedream end time vehicle specid 2 5 2014-04-23 09:00:00 2014-04-23 21:30:00 2 1 3 6 2014-04-25 13:00 : 00 2014-04-25 22:00:00 3 1 I want to ask the table, want to supply a start date time, end time and a space and find out Whether there is a booking for that booking at that time. I need to return the query right or wrong.
For example, if you contact me with current dataset:
startdatetime = 2014-04-23 09: 00: 00 and end time = 2014- 04-23 11:00:00 - The query will return right because the line ID2 defines the booking that exists at supply time. startdate = 2014-04-23 22:00:00 and end time = 2014-04-23 23:30:00 - the query will be incorrect because there are no bookings at that time is.
What you can do with a simple comparison and aggregation:
< Select> code> (in case when the count (*)> then 'book' with 'false' and 'true' end) where b.spaceid = @SPACEID and b.startdatetime & lt; @ENDTIME and b.endtatetime & gt; @ Time started; If one starts before the other end, and overlaps again, and ends at the start of the other.
Ideally, however, you want to set up the table so that there is no free space and can be booked at the same time. One way to do this is by using a table and a day for each location. For 48 slots (one for each half hour) will be. Instead of a line on each row, you will have a line for each time slot. After this you can do a unique barrier on spaceid, timelot and the database will apply this argument to you - possibly with a very low overhead rather than searching the table for overlapping reservations. .
Comments
Post a Comment