Room booking sql query

后端 未结 1 961
日久生厌
日久生厌 2021-02-10 00:51

I have a problem in writing the sql to get the available rooms from the tables. my table structures are given below.

table : booking

boo         


        
1条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-10 01:36

    This should do it; if there is a reservation that does not end before or start after the reservation we want, the room is considered busy.

    SELECT r.room_id
    FROM rooms r
    WHERE r.room_id NOT IN (
        SELECT b.room_id FROM bookings b
        WHERE NOT (b.end_datetime   < '2012-09-14T18:00'
                   OR
                   b.start_datetime > '2012-09-21T09:00'))
    ORDER BY r.room_id;
    

    SQLFiddle here.

    0 讨论(0)
提交回复
热议问题