简体   繁体   English

PHP / MySQL-检查双人会议室的预订

[英]PHP/MySQL - Checking for double meeting room booking

I want to find rows for anytime during the new period I'm requesting. 我想在我要求的新时间段内随时查找行。

Here is my table data which has existing rows: 这是我的表数据,具有现有行:

https://www.dropbox.com/s/1s4y8tl6s09emim/Screenshot%202014-04-04%2009.52.24.png

Here is the new period I'm requesting: 这是我要求的新时期:

Date: 2014-04-03 
Start Time: 12:30:00
End Time: 13:30:00

What is the correct SQL to find any existing rows for anytime during the new period I'm requesting? 在我请求的新时间段内随时可以找到任何现有行的正确SQL是什么?

I have this so far but not sure about the start_time and end_time fields. 到目前为止,我已经知道了,但是不确定start_timeend_time字段。

$sql = 'SELECT * FROM int_low_events WHERE start_date = "2014-04-03"';

我想您在表格中填写了date

 $sql = 'SELECT * FROM int_low_events WHERE date between '2014-04-03 12:30:00' and '2014-04-03 13:30:00'";
SELECT *, 
       ADDTIME(start_date, start_time) AS start,
       ADDTIME(end_date, end_time) AS end,
       '2014-04-03 14:00:00' AS proposed_start,
       '2014-04-03 16:05:00' AS proposed_end
  FROM int_low_events
HAVING (start BETWEEN proposed_start AND proposed_end OR end BETWEEN proposed_start AND proposed_end)
    OR (proposed_start BETWEEN start AND end OR proposed_end BETWEEN start AND end)

This is exactly the same as my previous (working) answer, but it is (in my opinion) a lot easier to understand. 这与我先前的(有效的)答案完全相同,但是(我认为)它更容易理解。 Also you now don't have to fill in the proposed start- and end-dates everywhere, but instead only once ( ... AS proposed_start and ... AS proposed_end ). 另外,您现在不必在任何地方填写建议的开始日期和结束日期,而只需填写一次( ... AS proposed_start... AS proposed_end )。

This does work according to this SQLFiddle with your database design. 根据您的数据库设计, 此SQLFiddle确实可以正常工作。


It needs to be so complicated, as it needs to fetch any event, that's running at the time of the new proposed event. 它需要非常复杂,因为它需要获取任何事件,而该事件在新建议的事件发生时正在运行。

id | start_date | start_time | end_date   | end_time
---+------------+------------+------------+-----------
62 | 2014-04-03 | 12:00:00   | 2014-04-05 | 13:00:00
63 | 2014-04-03 | NULL       | 2014-04-04 | NULL
64 | 2014-04-03 | 16:00:00   | 2014-04-03 | 17:00:00
65 | 2014-04-03 | 16:30:00   | 2014-04-03 | 17:30:00
66 | 2014-04-03 | 21:30:00   | 2014-04-03 | 22:30:00

This is the part of your database that matters. 这是数据库中重要的部分。 Assume we have a new event, that has the following dates: 2014-04-03 16:00 - 2014-04-03 18:00 . 假设我们有一个新事件,其日期如下: 2014-04-03 16:00 - 2014-04-03 18:00 With my previous query, it only selected the IDs 64 and 65 (as both of these have either the start_datetime OR end_datetime within our new event). 在我之前的查询中,它仅选择了ID 64和65(因为这两个ID在我们的新事件中都具有start_datetime或end_datetime)。

However ID 62 does also span over this timeframe, but starts earlier AND ends later. 但是,ID 62确实也跨越了这个时间范围,但开始时间较早,结束时间较晚。 With the new query, this will also be selected (if this makes any sense to you). 使用新查询时,也会选择该查询(如果这对您有意义)。

Here's an idea to check if two events are overlapping : you could use BETWEEN like this : 这是一个检查两个事件是否重叠的想法:您可以像这样使用BETWEEN

SELECT a.event_id, b.event_id 
FROM int_low_events AS a, int_low_events AS b
WHERE b.site_id = a.site_id AND
b.start_date = a.start_date AND
b.start_time BETWEEN a.start_time and a.end_time

This will work for events during less than one day, as the ones in your database screenshot. 这将适用于少于一天的事件,如数据库屏幕快照中的事件。

You may need to adapt it for events during more than one day. 您可能需要针对一天以上的活动进行调整。

So despite this being billed as a PHP question, it's actually a database question?? 因此,尽管这被称为PHP问题,但实际上是数据库问题?? ;-) ;-)

Hopefully your date-time combination are already encoded in the table as a date-time field, and not as two separate date and time fields. 希望您的日期时间组合已在表格中编码为日期时间字段,而不是两个单独的日期和时间字段。 If this is not the case, then I strongly suggest that you build a view which combines these two fields into one. 如果不是这种情况,那么我强烈建议您构建一个将这两个字段组合为一个视图。 Drastically simplifies your SQL, and your maintenance costs (for anyone trying to modify in the future). 大大简化了您的SQL和维护成本(对于将来尝试修改的任何人)。

If you don't do this, then you will have serious problems when searching on events which span several days, as is evident in other answers on this page. 如果您不这样做,那么搜索跨越几天的事件时就会遇到严重的问题,这在该页面的其他答案中也很明显。

Also, you need to change your incoming request to a date-time combination. 另外,您需要将传入请求更改为日期时间组合。 You can do this in your PHP code before binding to the SQL variable. 您可以在绑定到SQL变量之前在PHP代码中执行此操作。

The SQL then looks like this... 然后,SQL看起来像这样……

select *
from   int_low_events a
where  :proposed_start_datetime not between a.start_datetime and a.end_datetime
  and  :proposed_end_datetime not between a.start_datetime and a.end_datetime

-- edit -- Depending on which database you have underlying all this, there are different ways to combine the date and time fields. -编辑-根据所有这一切的基础数据库,可以使用不同的方式来组合日期和时间字段。 Use ADDTIME() if you're using MySQL. 如果您使用的是MySQL,请使用ADDTIME() Other databases will have their own ways of doing the same thing, thus allowing you to build the solution into your SQL rather than having to modify the database logic. 其他数据库将具有它们自己执行相同操作的方式,从而使您可以将解决方案构建到SQL中,而不必修改数据库逻辑。

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

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