MySQL如何检查两个日期之间的空周期
我有一个mysql表,如下所示:MySQL如何检查两个日期之间的空周期,mysql,sql,Mysql,Sql,我有一个mysql表,如下所示: | ID | date_start | date_end | | 1 | 2013-10-17 | 2013-10-18 | | 2 | 2013-10-28 | 2013-10-31 | 我需要检查两个日期之间的空周期 要从日期范围检索记录,我需要执行以下操作: SELECT * FROM `periods` WHERE ((date_start <= '2013-10-18' AND date_end >= '2013-10-18' A
| ID | date_start | date_end |
| 1 | 2013-10-17 | 2013-10-18 |
| 2 | 2013-10-28 | 2013-10-31 |
我需要检查两个日期之间的空周期
要从日期范围检索记录,我需要执行以下操作:
SELECT * FROM `periods` WHERE ((date_start <= '2013-10-18' AND date_end >= '2013-10-18' AND date_end <= '2013-10-31') OR (date_start >= '2013-10-18' AND date_start <= '2013-10-31' AND date_end >= '2013-10-31') OR (date_start >= '2013-10-18' AND date_end <= '2013-10-31') OR (date_start <= '2013-10-18' AND date_end >= '2013-10-31'))
使用Mysql的
DATEDIFF
函数:
SELECT DATEDIFF('2013-10-17','2013-10-18');
有关更多信息,请查看此链接:或 使用php:
floor(( strtotime( $endDate ) - strtotime( $startDate ) ) / 86400);
以下查询将为您提供重叠期间的开始日期。如果有多个,则中间有一个空时段
select distinct s.date_start
from periods s
left join periods e
on date_diff(s.date_start, e.date_end) <= 0
and date_diff(s.date_start, e.date_end) >= -1
where e.id is null
如果需要空时段的开始日期和结束日期,可以以类似的方式获取重叠时段的结束日期,然后再进一步 使用DATEDIFF这不能解决问题,或者我不明白。我需要检查两个日期之间的空周期,例如:日期范围2013-10-17至2013-10-31空周期为2013-10-19至2013-10-27。
select distinct s.date_start
from periods s
left join periods e
on date_diff(s.date_start, e.date_end) <= 0
and date_diff(s.date_start, e.date_end) >= -1
where e.id is null
echo (mysql_num_rows($result)>1) ? 'Empty period' : 'OK';