寻找有效期的房间

时间:2015-08-21 13:54:50

标签: php mysql database database-design

我有开始日期和结束日期。

 $s="YYYY-MM-DD";
 $e="YYYY-MM-DD";

我需要进行查询以检查该范围的可用房间。

 cObjects , cBookings_details
 cObjects_id = cBookings_details_cObjects_id

我真的被困在这里,真的很感激一些帮助。

我当前的查询

 SELECT cObjects.*
FROM cObjects LEFT JOIN cBookings_details ON cObjects.cObjects_id = cBookings_details.cBookings_details_cObjects_id
WHERE cBookings_details.cBookings_details_arrival NOT BETWEEN '".$s."' AND '".$e."' 
AND cBookings_details.cBookings_details_departure NOT BETWEEN '".$s."' AND '".$e."' 
AND cBookings_details.cBookings_details_arrival <> '".$s."' AND cBookings_details_departure <> '".$s."'
AND cBookings_details.cBookings_details_arrival <> '".$e."' AND cBookings_details_departure <> '".$e."'
AND cObjects.cObjects_type < '2'

OR cBookings_details.cBookings_details_id is null AND cObjects_type < '2'

1 个答案:

答案 0 :(得分:2)

这是一个常见的问题..实际上只是在工作中写了一个指南:

Selecting rows with start and end dates that feature in a chosen interval 
------------------------------------------------------------------------- 

Chosen Interval -  :start_date    (S) 
                   :end_date      (E)

Rows            -  row.start_date (s)
                   row.end_date   (e)

Increasing date-->

         S       E
         |       |           Chosen Interval 

1.     s---e                 Rows we want
2.        s---e                    .
3.            s---e                .
4.     s----------e                .

5.  s--e                     Rows we don't want
6.                s--e             .

LOGIC:
row.end_date >= :start_date AND row.start_date <= :end_date   

如果您正在寻找可用的房间,LEFT JOIN使用上面的逻辑和预订SELECT的{​​{1}}行预订的房间。

应用于您的架构我相信这会出现:

NULL

顺便说一句,如果我必须使用你的架构,我会哭。为什么要重复列中的表名?它只会增加噪音。