用于表预留的两个日期时间之间的SQL命令

时间:2014-01-21 10:10:39

标签: mysql sql datetime

我正在开发餐厅预订系统,如果有人想要来,必须进行预订。

这意味着主人进入到达和离开,所以主人想要进入餐厅的确切时间间隔。

So if that customer A has a reservation 8:00 p.m. to 10:00 p.m. and customer arrives B, who wants to come 9:00 p.m. to 11:00 p.m. and wants to book the same table as the customer A system will not let go, because the times overlap .

$GuetComming = '2014-01-21 12:30:00'
$GuestDepature = '2014-01-21 16:30:00'
$i= '10'

SQL查询:

SELECT * FROM `booking`
    where (Table='$i' AND Comming BETWEEN '$GuestComming' AND '$GuestDepature')
    or (Table='$i' AND Depature BETWEEN '$GuestComming' AND '$GuestDepature')
    or (Table='$i' AND Comming <= '$GuestComming' AND Depature >='$GuestDepature')
    or (Table='$i' AND Comming BETWEEN '$GuestComming' AND '$GuestDepature')

我目前的命令有效,但我相信会更容易。

This is how it works, but unfortunately it happens sometimes that a table is available when it is reserved.

因此,寻求更好的解决方案,因为餐厅是approximately 100 tables,我相信有时可能发生服务器无法找出是否保留了一张桌子。

1 个答案:

答案 0 :(得分:2)

我会把它减少到以下:

SELECT * FROM `booking`
WHERE Table='$i' AND NOT(Comming >= '$GuestDepature' OR Depature <= '$GuestComming')

如果要记录表,查询结果应为空。不确定'odchodHosta'字段是如何工作的。

//更新SQL-添加NOT