简体   繁体   中英

SQL find two consecutive days in a reservation system

Extending on this question: hotel reservation system SQL query: identify when one specific room is available

Using the schema listed in the question above, how can I have a query that says "Find me a room for 2 consecutive days thats available in this week?"

Just join to the availability table twice

SELECT rooms.* FROM rooms, availability as a1, availability as a2
WHERE rooms.id = 123
AND a1.room_id = rooms.id
AND a2.room_id=  rooms.id
AND a1.date_occupied + 1 = a2.date_occupied

or, if we're not into writing SQL like its 1985:

SELECT rooms.* FROM rooms
JOIN availability a1 on a1.room_id = rooms.id
Join availability a2 on a2.room_id = rooms.id AND a1.date_occupied + 1 = a2.date_occupied
WHERE rooms.id = 123

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM