這是我的查詢,你需要它
$sql = "SELECT COUNT(*) as num FROM meeting_room_bookings
WHERE
(
(startdate < start_at AND startdate > start_at)
OR
(startdate > start_at AND enddate < end_at)
)
AND
meeting_room_id = whatever_id i was on about at the time :P";
如果你要消毒的數據,那麼你將需要更換的開始日期和結束日期值只需編輯值「?」而這樣做這個 - >
$sql = "SELECT COUNT(*) as num FROM meeting_room_bookings
WHERE
(
(? < start_at AND ? > start_at)
OR
(? > start_at AND ? < end_at)
)
AND
meeting_room_id = ?";
$result = p4a_db::singleton()->fetchRow($sql, array($date, $date, $date, $dateend, $merono));
走得更遠之一:
$sql = "SELECT COUNT(*) as num FROM meeting_room_bookings
WHERE
(
(start_at >= ? AND start_at <= ?)
OR
(start_at <= ? AND end_at >= ?)
OR
(end_at >= ? AND end_at <= ?)
OR
(? >= ?)
)
AND
meeting_room_id = ?";
$result = p4a_db::singleton()->fetchRow($sql, array($date, $dateend, $date, $dateend, $date, $dateend, $date, $dateend, $merono));
這種方法可以讓沒有日期過來的字段或任何其他可能的領域之外的領域內該區域(見圖):
|| |-----------| || first booking
|-----| ok
|---| fails
|----| fails
|----| fails
如果你需要一個完整的解釋去This Topic
,當你做了錯誤的信息,你需要同時做,如果其他人所以這句法應該做的工作:
if (0 == $result["num"])
{
p4a_db::singleton()->query("INSERT INTO meeting_room_bookings (start_at, end_at, meeting_room_id, tennant_id, centre_id)
VALUES
(?, ?, ?, ?, ?)", array($date, $dateend, $merono, $row['tennant_id'], $centreRow['id']));
return true;
}
else
{
P4A::singleton()->messageError("The selected times conflict with another booking. Please select another time or meeting room!");
}
請忽略與中心ID和坦ID字段(除非你使用這些)他們在那裏確保預訂設置在正確的tennant和正確的會議室。根據需要使用它並替換你需要的東西。
順便說一句,我使用P4A應用程序框架,所以你需要用PHP錯誤信息mobober
怎麼樣預訂當天/ S替換
P4A::singleton()->messageError
?你只想檢查時間尺度? – mallix數據庫中的開始時間/結束時間列是否爲適當的「DATETIME」或「TIME」類型,或者它們是否是varchar類型? –
我想你所問的與現有的有關。 [http://stackoverflow.com/questions/11098541/check-if-two-times-overlap][1] [1]:http://stackoverflow.com/questions/ 11098541/check-if-two-times-overlap – nTony