简体   繁体   English

Mysql查询,看看预订是否会与另一个预订发生冲突。

[英]Mysql query to see if a booking will conflict with another booking.

I have a table which stores a bunch of books. 我有一张桌子可以存放一堆书。 Customers can book appointments at for any time during the day. 客户可以在白天的任何时间预约。 I want to make sure that a booking doesnt already exist that will conflict with the new booking. 我想确保预订不存在会与新预订冲突。

So I want to find the booking on that date and check that the start and end times dont overlap. 所以我想在那个日期找到预订,并检查开始和结束时间是否重叠。 I tried doing this with a sub select query to get the bookings from that day and then see if those times overlapped but it didnt seem to be working. 我尝试使用子选择查询来获取当天的预订,然后查看这些时间是否重叠但它似乎没有起作用。

Here is the query I am using. 这是我正在使用的查询。 (Status and active are some other fields I need). (状态和活动是我需要的其他一些领域)。 I find the relevant dates and then see if 8am is between those results individual start/end times or is 9.40am also falls between start/end times. 我找到相关的日期,然后看看早上8点是在这些结果之间的个别开始/结束时间还是在上午9点40分之间也是在开始/结束时间之间。

SELECT event_date, starttime, endtime, status, active 
FROM (SELECT event_date, starttime, endtime, status, active FROM bookings WHERE event_date='2013-07-21' AND status != 'cancelled' AND active !=0 LIMIT 1) AS q1
WHERE ('8:00:00' BETWEEN q1.starttime AND q1.endtime) AND ('9:40:00' BETWEEN q1.starttime AND q1.endtime)

Is this the best method to use? 这是最好的使用方法吗? Thanks 谢谢

WHERE ('8:00:00' BETWEEN q1.starttime AND q1.endtime) AND ('9:40:00' BETWEEN q1.starttime AND q1.endtime)

This implies that the new appointment (8 to 9:40) lies ENTIRELY INSIDE an already existing appt. 这意味着新约会(8至9:40)完全位于现有的appt内。 Instead, you want (I think) to see if either the start OR end of the appt coincides with another one: 相反,你想(我认为)看看appt的开头或结尾是否与另一个重合:

WHERE ('8:00:00' BETWEEN q1.starttime AND q1.endtime) OR ('9:40:00' BETWEEN q1.starttime AND q1.endtime)

Also, I'm pretty sure that inner select is unnecessary. 另外,我很确定内部选择是不必要的。 I believe this will have the same effect: 我相信这会产生同样的效果:

SELECT TOP 1 event_date, starttime, endtime, status, active
FROM bookings 
WHERE event_date='2013-07-21' 
    AND status != 'cancelled' 
    AND active != 0
    AND (('8:00:00' BETWEEN q1.starttime AND q1.endtime)
        OR ('9:40:00' BETWEEN q1.starttime AND q1.endtime))
-- if you get a row, then there is a conflicting appt.

To check if one range overlaps another it's easier to think first when it won't overlap and then reverse the condition. 要检查一个范围是否与另一个范围重叠,当它不重叠然后反转条件时,更容易先思考。 It's obvious that: 很明显:

Two ranges (A,B) don't overlap if A starts after B ends, or if A ends before B starts. 如果A在B结束后开始,或者如果A在B开始之前结束,则两个范围(A,B) 重叠。

Turning this to SQL you get the following condition for finding an overlapping range: 将此转换为SQL,您将获得以下条件来查找重叠范围:

... WHERE NOT ('8:00:00' > q1.endtime OR '9:40:00' < q1.starttime)

If you want you can rewrite that as: 如果你想要你可以重写为:

... WHERE q1.endtime > '8:00:00' AND q1.starttime < '9:40:00'

This way you handle all the corner cases, like one interval completely containing the other. 这样您就可以处理所有极端情况,例如一个完全包含另一个的区间。

I ran in the same problem and i used the posted solution but there is another bug that i found if the previous record was lie in the new record as example 我遇到了同样的问题,我使用了发布的解决方案,但是如果前一条记录位于新记录中,我发现了另一个错误示例

The record in the database is starttime 8:30:00 endtime 9:00:00 数据库中的记录是starttime 8:30:00 endtime 9:00:00

In this case the record will be added as no matching rows will be found with this 在这种情况下,将添加记录,因为不会找到匹配的行

SELECT TOP 1 event_date, starttime, endtime, status, active
FROM bookings 
WHERE event_date='2013-07-21' 
    AND status != 'cancelled' 
    AND active != 0
    AND (('8:00:00' BETWEEN q1.starttime AND q1.endtime)
        OR ('9:40:00' BETWEEN q1.starttime AND q1.endtime))

to fix this issue you need check if the start time or end time it self lies between your new entry like this 要解决这个问题,你需要检查一下这个新条目之间的开始时间或结束时间

SELECT TOP 1 event_date, starttime, endtime, status, active
FROM bookings 
WHERE event_date='2013-07-21' 
    AND status != 'cancelled' 
    AND active != 0
    AND (('8:00:00' BETWEEN q1.starttime AND q1.endtime)
        OR ('9:40:00' BETWEEN q1.starttime AND q1.endtime)
        OR (q1.starttime BETWEEN '8:00:00' AND '9:40:00')
        OR (q1.endtime BETWEEN 8:00:00' AND '9:40:00')
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM