我在数据库中有一个名为 reservations 的表,其中包含以下列:id、date_from、date_to、< em>obj_id。
在我的应用程序中,我需要创建一个 SQL 查询,在其中可以检查指定日期范围内房间 (obj_id) 的可用性。
我尝试使用类似以下的内容,但效果不佳:
select * from `reservations`
where ((`date_from` <= $from and `date_to` >= $from)
or (`date_from` <= $to and `date_to` >= $to)) and `obj_id` = $obj
例如,当2013-07-03至2013-07-06期间预订了某些房间时,2013-07-01至2013-07-07的日期将被视为免费
最佳答案
您可以使用sql的Between子句来过滤两个给定日期之间的结果。看这个link了解详情。
您可以尝试以下查询!
Select * from `Reservations` where $ToDate between date_from and date_to
OR date_to between $FromDate and $ToDate
OR $FromDate between date_from and date_to
OR date_from between $FromDate and $ToDate
AND `obj_id` = $obj;
关于mysql - 查看日期范围内所选房间的空房情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17565582/