简体   繁体   English

SQL:按日期范围查找数据

[英]SQL : find data by range of date

I have 2 records with field "from_date" and "to_date" : 我有2条记录,其中包含字段“ from_date”和“ to_date”:

 - Record 1 : from_date=2017-05-15 and to_date=2017-06-12
 - Record 2 : from_date=2018-03-20 and to_date=2018-04-11

how to get Record 2 if search from date : 如果从日期开始搜索,如何获取记录2:

 - 2018-03-01 and 2018-03-31?

or 要么

 - 2018-04-01 and 2018-04-30?

or 要么

 - 2018-04-01 and 2018-04-03?

This is as simple as : 这很简单:

SELECT * 
   FROM records 
    WHERE from_date >= @from_date 
         OR to_date <= @to_date 

Where @from_date and @to_date are just your variables. 其中@from_date@to_date只是您的变量。

I used an OR since you are not looking for a inclusive range. 我使用了OR因为您没有在寻找包含范围。 It is just a partial match. 这只是部分比赛。

You want a condition that match this record from_date=2018-03-20 and to_date=2018-04-11 您想要一个条件与此记录from_date=2018-03-20 to_date=2018-04-11to_date=2018-04-11匹配的条件

Let's review the condition based on WHERE from_date >= @from_date OR to_date <= @to_date 让我们根据WHERE from_date >= @from_date OR to_date <= @to_date

@from_date = 2018-03-01 --false
@to_date   = 2018-03-31 --true
--
@from_date = 2018-04-01 --true
@to_date   = 2018-04-30 --false
--
@from_date = 2018-04-01 --true
@to_date   = 2018-04-03 --true

This shows that you only need one of the bounderies to match. 这表明您只需要一个边界即可匹配。

Note that based on the DBMS, the date comparison can vary, but the logic remain the same. 请注意,基于DBMS,日期比较可能会有所不同,但是逻辑保持不变。

Try this sample: 试试这个例子:

Declare @date1 Date
Declare @date2 Date

set @date1 = <<give your first date>> 'yyyy-dd-mm
set @date2 = <<give your second date>> 'yyyy-dd-mm

SELECT * FROM tbldate WHERE CONVERT(DATE,@date1) BETWEEN from_date and to_date OR CONVERT(DATE,@date2) BETWEEN from_date and to_date

oracle example: oracle示例:

with 
    table1 as 
      ( select 1 id, to_date('2017-05-15','YYYY-MM-DD') date_from, to_date('2017-06-12','YYYY-MM-DD') date_to  from dual union all
        select 2 id, to_date('2018-03-20','YYYY-MM-DD') date_from, to_date('2018-04-11','YYYY-MM-DD') date_to  from dual )
select
    *
from 
    table1
where
/*
        to_date('2018-03-01','YYYY-MM-DD') < date_to
    and to_date('2018-03-31','YYYY-MM-DD') > date_from

        to_date('2018-04-01','YYYY-MM-DD') < date_to
    and to_date('2018-04-30','YYYY-MM-DD') > date_from
*/
        to_date('2018-04-01','YYYY-MM-DD') < date_to
    and to_date('2018-04-03','YYYY-MM-DD') > date_from
;

Thank you all for your response. 谢谢大家的回应。 I have found the answer. 我找到了答案。

WHERE ((from_date BETWEEN {fromDate} AND {toDate} OR to_date BETWEEN {fromDate} AND {toDate} OR (from_date <= {fromDate} AND to_date >= {fromDate}))

if using CakePHP3 : 如果使用CakePHP3:

->where([
        'OR' => [
            function($exp) use($fromDate,$toDate){                            
                return $exp->between('from_date',$fromDate,$toDate,'date');
            },
            function($exp) use($fromDate,$toDate){                            
                return $exp->between('to_date',$fromDate,$toDate,'date');
            }, 
            'AND' => [
                'from_date <=' => $fromDate,
                'to_date >=' => $fromDate
            ]
        ],
        'from_date is not' => null
    ]);

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

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