Find free room (booking system)

前端 未结 2 1435
独厮守ぢ
独厮守ぢ 2021-01-03 07:03

For a booking system I have a table with rooms, arrival and departure.

Example data:

id | room | arrival    | departure
---+------+------------+-----         


        
相关标签:
2条回答
  • 2021-01-03 07:50

    Here is a query that will show the NOT-FREE rooms for a date span:

    select room from bookings where
    (arrival<'2011-03-12' and departure>='2011-03-12') -- overlap at the end
    OR (arrival<='2011-03-10' and departure>'2011-03-10') -- overlap at the start
    OR (arrival>='2011-03-10' and departure<='2011-03-12') -- complete overlap
    

    You can use this with

    select roomnumber from rooms where roomnumber not in (... as above ...)
    

    to find the FREE rooms

    0 讨论(0)
  • 2021-01-03 07:51

    You could also use the BETWEEN comparison operator for this purpose. In this case you would do something like this:

    SELECT r.id FROM room r WHERE r.id NOT IN
    (
        SELECT rb.room FROM room_booking rb WHERE 
            ('2011-03-10' BETWEEN rb.arrival AND rb.departure) OR 
            ('2011-03-12' BETWEEN rb.arrival AND rb.departure)
    )
    
    0 讨论(0)
提交回复
热议问题