Mysql SQL:按日期范围查找数据
我有两个记录,字段分别为“开始日期”和“结束日期”:Mysql SQL:按日期范围查找数据,mysql,sql,Mysql,Sql,我有两个记录,字段分别为“开始日期”和“结束日期”: - 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 如果从日期开始搜索,如何获取记录2: - 2018-03-01 and 2018-03-31? 或 或 这很简单: SELECT * FROM records WHERE from_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
如果从日期开始搜索,如何获取记录2:
- 2018-03-01 and 2018-03-31?
或
或
这很简单:
SELECT *
FROM records
WHERE from_date >= @from_date
OR to_date <= @to_date
这表明您只需要一个bounderies来匹配
请注意,基于DBMS,日期比较可能会有所不同,但逻辑保持不变。尝试以下示例:
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示例:
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
;
谢谢大家的回复。我找到了答案
WHERE ((from_date BETWEEN {fromDate} AND {toDate} OR to_date BETWEEN {fromDate} AND {toDate} OR (from_date <= {fromDate} AND to_date >= {fromDate}))
如果使用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
]);
这取决于你想要的条件。您的记录中有一个日期范围,但也提供了一个条件的日期范围,该日期是包含的还是独占的?请提供完整的输入/输出示例。当然,你试过什么吗?@AxelH,我只想获得上述所有条件的记录2。事实上,我自己已经找到了答案。谢谢,顺便说一句。@a_horse_,没有名字,我正在使用mysql
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
;
WHERE ((from_date BETWEEN {fromDate} AND {toDate} OR to_date BETWEEN {fromDate} AND {toDate} OR (from_date <= {fromDate} AND to_date >= {fromDate}))
->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
]);