mysql检查其他日期范围内的日期范围

时间:2014-03-11 09:15:30

标签: php mysql date range

我有一行日期范围

title | fromDate | tillDate
------------------------------------
my event | 2014-03-12 | 2014-03-13

my event 2 | 2014-03-14 | 2014-03-17

my event 3 | 2014-03-01 | 2014-03-10

my event 4 | 2014-03-01 | 2014-03-09

现在我有第二对日期范围,我想检查事件是否在日期范围内..例如

checkStartDate = 2014-03-10 checkEndStartDate = 2014-03-14

所以在这个范围内将是“我的活动”,“我的活动2”和“我的活动3”......

此刻我想尝试获取我的范围之间的所有日期(所以在2014-03-10和2014-03-14之间)并为它写一个大查询......但必须有一个更容易这样做的方法对吗?

3 个答案:

答案 0 :(得分:1)

SELECT title FROM table WHERE (DATE('2014-03-10') BETWEEN DATE(fromDate) AND DATE(tillDate) OR DATE('2014-03-14') BETWEEN DATE(fromDate) AND DATE(tillDate) OR (DATE('2014-03-10') <= DATE(fromDate) AND DATE('2014-03-14') >= DATE(tillDate)))

答案 1 :(得分:0)

试试这个:

select title from table
where checkStartDate  <= fromDate 
and checkEndStartDate <= tillDate

我已经编辑了我的答案。

答案 2 :(得分:0)

使用此代码

select *  from events_table  where fromDate >= checkStartDate   and tillDate <= checkEndStartDate