Php 检查可用性并列出可用的房间
这些天来,我正在努力学习我的酒店客房预订系统学位项目Php 检查可用性并列出可用的房间,php,mysql,Php,Mysql,这些天来,我正在努力学习我的酒店客房预订系统学位项目 现在,我在检查可用性阶段努力工作 这是我的桌子结构 预订 `ID` int(11) `roomID` int(11) `startDate` date `endDate` date 房间桌子 `roomID` int(11) `name` varchar(255) `facilities` mediumtext 假设有5个房间。室友是01,02,03,
现在,我在检查可用性阶段努力工作
这是我的桌子结构
预订
`ID` int(11)
`roomID` int(11)
`startDate` date
`endDate` date
房间桌子
`roomID` int(11)
`name` varchar(255)
`facilities` mediumtext
假设有5个房间。室友是01,02,03,04和05。
在这些房间中,有01间已预订。入住日期为2013年11月5日,结账日期为2013年11月10日。
还有04号房间,预订时间为2013年11月8日至2013年11月9日
如果用户检查2013年11月4日至2013年11月10日期间的可用性,则仅显示房间ID 02、03和05
如何构建显示结果的系统?
有人能帮我建立sql查询来检查可用性吗?这里的逻辑实际上很简单,你不需要任何开始和结束在你的周期内的地方,或者它们在你要寻找的范围内的任何点的对面
SELECT
R.roomID,
R.name,
R.facilities
FROM
-- Use the table rooms, but name it R for shorthand.
Rooms R
-- Left joins return null if there's no match, which we use in the where clause to identify these rows
LEFT JOIN Bookings B
-- B is bookings, R is rooms, they're aliased to make things easier
ON B.RoomId = R.RoomId
AND
(
-- As I said at the top of this answer, we want to exclude anything that matches any of 3 conditions:
-- The start date is between the date's the room is already booked for
@SearchCheckingStart BETWEEN B.startDate AND B.endDate
OR
-- The end date is in the period where the room is already booked
@SearchCheckingEnd BETWEEN B.startDate AND B.endDate
OR
-- Or our date range lies between the period we're booking for
B.startDate BETWEEN @SearchCheckingStart AND @SearchCheckingEnd
)
WHERE -- We're only interested in results where MySQL couldn't match the results earlier, this gives us the rooms that are free.
B.RoomId IS NULL
第一步。学习SQL。第二步。尝试我还是SQL的初学者。这就是为什么我问你Google有很多SQL教程给你,StackOverflow是针对特定的编程问题(意味着你必须进行编码尝试)正如你从评论中可以看出的那样,OP几乎没有SQL方面的经验。为了让这成为一个好的答案,您必须解释这个查询的工作原理,包括连接、连接和可能的别名。(这就是当你回答糟糕的问题时会发生的情况)亲爱的斯克拉加非常感谢你宝贵的回答。你能简要解释一下最后10行吗。plz-plz-plz