Checking for date range conflicts in MySQL

前端 未结 4 1493
不思量自难忘°
不思量自难忘° 2021-02-02 03:25

I am writing a hotel booking system. after lots of studying (including stack overflow) i wrote this sql to find out free rooms:

SELECT
*
FROM room
WHERE
    room         


        
4条回答
  •  孤城傲影
    2021-02-02 03:49

    Your original logic was very close, you just need to swap the '$check_in' and '$check_out' values. I.e.:

    SELECT *
    FROM room
    WHERE room_id NOT IN
    (
        SELECT room_id
        FROM bookings
        WHERE checkin <= '$check_out' AND checkout >= '$check_in'
    )
    

    Brian Driscoll's answer focusses on the scenarios that constitute booking conflicts, as so:

    ---------------|-----Booked-----|---------------
           |----A1----|
                                 |----A2----|
                        |--A3--|
                |----------A4----------|
    
    Case A2 & A3: checkin <= '$check_in' AND checkout >= '$check_in'
    Case A1 & A3: checkin <= '$check_out' AND checkout >= '$check_out'
    Case A4:      checkin >= '$check_in' AND checkout <= '$check_out'
    

    However the senarios that constitute no conflict are much simpler. There are only two:

    ---------------|-----Booked-----|---------------
      |----B1----|                             
                                      |----B2----|
    
    Case B1: checkin > '$check_out'
    Case B2: checkout < '$check_in'
    

    So the situation where there is no conflict between a booking and a potential booking can be expressed with this SQL:

    checkin > '$check_out' OR checkout < '$check_in'
    

    To check for conflicts instead, we just need to negate this. So, using DeMorgans Law, the negation is:

    checkin <= '$check_out' AND checkout >= '$check_in'
    

    ...which arrives at the solution given above.

提交回复
热议问题