我有一个这样的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' 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'))
在 php 中,我可以通过 while 循环检查它,但是是否有可以检查的 mysql 查询?
$first = true;
$emptyPeriod = false;
while ($period = mysql_fetch_assoc($result)) {
if($first) {
$first = false;
$prevEndPeriod = $period['date_end'];
continue;
}
$nextDay = date('Y-m-d', strtotime($prevEndPeriod . ' + 1 day'));
if($nextDay != $period['date_start']) {
$emptyPeriod = true;
break;
} else {
$prevEndPeriod = $period['date_end'];
}
}
echo ($emptyPeriod) ? 'Empty period' : 'OK';