简体   繁体   English

SQL查询预订日期从和到

[英]SQL Query booking date between from and to

Looking for a sql query that return the data based on the from and to date.寻找基于起始日期和截止日期返回数据的 sql 查询。

Table :桌子 :

id - int
carid int,
bookingStartDate - datetime
bookingEndDate - datetime

Here all the following query that i tried to test but all its get fail to retun the expected result在这里,我尝试测试的所有以下查询都无法重新调整预期结果

Example 1:示例 1:

set @startdate='2019-12-21'
set @enddate='2019-12-22'

--Example 1
select  id,bookingStartDate,bookingEndDate from orders
where  
(bookingStartDate between @startdate  and @enddate
and bookingEndDate between @startdate and @enddate)


--Example 2
select  id,bookingStartDate,bookingEndDate from orders
where 
status=4 and 
(bookingStartDate >=@startdate and bookingEndDate <= @enddate)

--Example 3
select  id,bookingStartDate,bookingEndDate from orders
where 
(bookingStartDate <= @startdate  and bookingEndDate >= @startdate)
or 
(bookingStartDate <= @enddate and bookingEndDate >= @enddate)

The requirement is, To check list all available cars based on the from date and todate要求是,根据起始日期和今天检查列表所有可用汽车

This is the overlapping range problem, and your query should look something like this:这是重叠范围问题,您的查询应如下所示:

SELECT id, bookingStartDate, bookingEndDate
FROM orders
WHERE
    @startdate < bookingEndDate AND
    @enddate   > bookingStartDate;

This would find all available reservations which are able to contain the date range defined by @startdate to @enddate .这将找到能够包含由@startdate@enddate定义的日期范围的所有可用预订。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM