2014-12-02 77 views
0

我有一台名为“checkInGuestUsers”,其中有像“checkInTime(DATETIME),checkOutTime(DATETIME),客房(VARCHAR)列MYSQL:查找特定时期之间记录

还有谁订一些特定的用户客房特定时间内,

UserId checkInTime   checkOutTime    rooms 

25  2014-12-04 00:00:00 2014-12-05 23:59:59  101,102 

现在其他用户或同一用户想要内

"2014-12-03 00:00:00" to "2014-12-04 23:59:59" OR 
"2014-12-04 00:00:00" to "2014-12-04 23:59:59" 

预订房间,然后系统将返回所有的房间里除101,102,因为这些房间ST已经预订了 “2014年12月4日00:00:00”到“2014年12月4日23:59:59”

我想要一个SQL查询一样,如果我选择的时间从上述两者中选择一个返回在该时段内预订的房间。

我已经尝试过使用之间和大于或小于条件在SQL中但未能得到确切的结果。

回答

0

从房间选择其中ROOM_ID是checkInTime空或checkOutTime < = bookFrom和checkInTime为空或bookTo < = checkInTime

这是功课?

+0

在你已经给出这个问题的答案之后,迟一点问问。 – EternalHour 2014-12-02 07:35:05

+0

是的,我永远不知道我是否应该......主要是为了羞愧他们。该网站是否有推荐的政策? – Andras 2014-12-02 07:41:14

0
select r.* 
from rooms r 
    left join checkInGuestUsers g 
    on g.rooms like r.<room_no> -- actually not a good condition, should be improved 
    and g.checkInTime <= {:book_to} and g.checkOutTime >= {:book_from} 
where g.rooms is null