检查日期是否属于范围日期

时间:2015-05-22 21:22:10

标签: mysql sql

我正在尝试做一个简单的预订系统,我想有一个SQL查询来检查日期是否可用以及日期范围内没有日期。

示例:此日期为:2016-01-01 - > 2016年1月8日 我必须查看日期:

  • 2015-12-25 - 2016-01-03
  • 2015-12-25 - 2016-01-20
  • 2016-01-04 - 2016-01-06
  • 2016-01-04 - 2016-01-10
  • 2016-01-01 - 2016-01-08

这些是我想到的场景。但在研究时,所有的例子都有2套日期需要检查。所有方案都不是从数据库中检查未知日期。 我搞不清楚了。如果有人可以提供帮助我会很感激。也许你过去曾经使用过这个场景。

4 个答案:

答案 0 :(得分:0)

当s1< = e2和e1> = s2时,两个句点(s1,e1)和(s2,e2)重叠

SELECT * FROM tab
WHERE checkInDate <= DATE '2016-01-03' AND checkOutDate >= DATE '2015-12-25';

当s1&lt; = s2和e1&gt; = e2时,句点(s1,e1)中的句点(s2,e2)包含

SELECT * FROM tab
WHERE checkInDate <= DATE '2015-12-25' AND checkOutDate >= DATE '2016-01-03';

fiddle

答案 1 :(得分:0)

For future reference for anyone who has this problem, here is how I solved my issue :

$query  = "SELECT * FROM booking WHERE ";
        $query .= "checkInDate >= $chkInDate AND checkOutDate >= $chkOutDate OR ";
        $query .= "checkInDate >= $chkInDate AND checkOutDate <= $chkOutDate OR ";
        $query .= "checkInDate <= $chkInDate AND checkOutDate >= $chkOutDate OR ";
        $query .= "checkInDate <= $chkInDate AND checkOutDate <= $chkOutDate OR ";
        $query .= "checkInDate =  $chkInDate AND checkOutDate  = $chkOutDate ";

答案 2 :(得分:0)

适用于我和我的查询的示例。它始终准确无误。

canvas.getAnimator().setUpdateFPSFrames(60, null);

使用这些功能:

SET @PerBeg = ’2010-01-01’;
SET @PerEnd = ’2010-01-31’;

SELECT  
    Reservation.ArrivalDate AS ArrivalDate, 
    Reservation.DepartureDate AS DepartureDate, 
. . . 

WHERE

fBoolActyInPeriod(ArrivalDate,DepartureDate,@PerBeg,@PerEnd)

答案 3 :(得分:-1)

你可以这样做:

SELECT * FROM reserves r
WHERE 
    start_dt < '2016-01-03' and end_date > '2015-12-25'
or start_dt < '2016-01-20' and end_date > '2016-01-04'
or start_dt < '2016-01-06' and end_date > '2016-01-04'
or start_dt < '2016-01-10' and end_date > '2016-01-04'
or start_dt < '2016-01-08' and end_date > '2016-01-01'
LIMIT 1 

如果有任何记录返回,则这些日期有保留。您可以在应用程序中以编程方式创建此SQL,无论您需要哪个句点。

如果您需要查看此期间的储备金,请删除LIMIT 1条款。