简体   繁体   English

酒店客房预订/预订

[英]Hotel room booking/reservation

I'm working on a reservation/booking system for a small hotel. 我正在为一家小旅馆预订/预订系统。 I'm pretty good with PHP but not so good with SQL... I made a form where you enter your information, number of rooms and select arrival date and check-out date using a calendar. 我对PHP相当满意,但对SQL却不太满意...我做了一个表格,您可以在其中输入您的信息,房间数量,并使用日历选择到达日期和退房日期。

Now everything went good until I got to the point where you have to check which rooms are available and it's giving me a headache. 现在一切都进行得很好,直到我到达必须检查哪些房间可用的地步,这让我头疼。 There are 10 rooms you can book. 您可以预订10间客房。

I currently have one table in MySQL storing the information, dates, booking-ID and room-ID/number. 我目前在MySQL中有一张表,用于存储信息,日期,预订ID和房间ID /编号。

How would you make the SQL for checking which rooms that are available and not? 您将如何使用SQL来检查哪些房间可用和不可用?

Should it look something like 它看起来像什么

"SELECT * FROM bookings WHERE checkinDate >= '$formCheckin' 
 AND checkoutDate <= '$formCheckout' " 

and then get the roomID and count them? 然后获取roomID并进行计数?

Any help is very appreciated! 任何帮助都非常感谢!

If you want to know if a room is available during a period, then the logic looks like: 如果您想知道一段时间内是否有空房间,则逻辑如下:

SELECT r.*
FROM rooms r 
WHERE NOT EXISTS (SELECT 1
                  FROM bookings b
                  WHERE b.roomid = r.roomid AND
                        b.checkinDate <= $formCheckOut AND
                        b.checkoutDate >= '$formCheckIn
                 );

I'm not sure if the equality comparison is needed for both of these. 我不确定这两个是否都需要相等比较。 The logic is that a room is available if there are no bookings that start before the checkout date and that end after the check in date. 逻辑是,如果没有预订在结帐日期之前开始且在结帐日期之后结束的预订,则该房间可用。

However, for ten rooms, I might suggest that you just keep a table of each room by day, say for the next ten years (add another year once per year). 但是,对于十个房间,我建议您每天仅保留每个房间的一张桌子,比如说未来十年(每年再增加一次)。 Such a table isn't very big and it is probably easier to understand how to use it. 这样的表不是很大,可能更容易理解如何使用它。 Plus, you can handle things like a couple reserve the room for a week, but only one person is in the room for the first 3 days. 另外,您可以处理一些事情,例如一对夫妇预订房间一周,但前三天只有一个人在房间里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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