之間的空白時期我有一個MySQL表是這樣的:的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查詢?的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';
使用DATEDIFF http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff – Ashalynd