扩展这个问题: hotel reservation system SQL query: identify when one specific room is available
使用上面问题中列出的架构,我怎样才能有一个查询“在本周连续2天找到我的房间?”
答案 0 :(得分:1)
只需加入可用性表两次
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
或者,如果我们不像1985年那样编写SQL:
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