简体   繁体   English

在mysql中的开始时间和结束时间之间获取30分钟的间隔数据

[英]Get 30 minutes interval data between start and end time in mysql

I have table structure in mysql, 我在mysql中有表结构,

table_id  no_people  booking_date  bookingend_time      bookingstart_time
      14          2  2014-10-31    2014-10-31 13:30:00  2014-10-31 11:00:00
       5          4  2014-10-31    2014-10-31 16:30:00  2014-10-31 14:30:00
       6          2  2014-10-31    2014-10-31 17:00:00  2014-10-31 16:00:00
       2          4  2014-11-06    2014-11-06 12:30:00  2014-11-06 10:00:00
       2          4  2014-10-31    2014-10-31 16:00:00  2014-10-31 14:00:00
       3          4  2014-11-01    2014-11-01 09:00:00  2014-11-01 07:30:00
       6          2  2014-11-01    2014-11-01 10:00:00  2014-11-01 07:30:00
       2          4  2014-11-03    2014-11-03 10:30:00  2014-11-03 08:30:00
       5          4  2014-11-04    2014-11-04 10:30:00  2014-11-04 08:30:00
       3          4  2014-11-05    2014-11-05 09:30:00  2014-11-05 07:30:00
      14          2  2014-11-05    2014-11-05 09:30:00  2014-11-05 07:30:00

I want to retrieve table_id data with 30 minutes of interval between start and end time. 我想以30分钟的开始和结束时间间隔检索table_id数据。

Ex: if i give booking start time 10:30 and end time 12:30 i should get 14 as row.. Similarly it should check all rows and return between two times .. 例如:如果我给预订开始时间10:30和结束时间12:30,我应该得到14作为行。同样,它应该检查所有行并在两次之间返回。

My query so far 到目前为止我的查询

SELECT `table_id` FROM `booking` WHERE bookingstart_time>='2014-10-31 10:30:00' AND bookingend_time<='2014-10-31 11:30:00'

Step 1: expand the input time frame by 30 minutes before and 30 minutes after. 步骤1:将输入时间范围扩大30分钟之前和30分钟之后。 DATE_ADD() and DATE_SUB() can do that: DATE_ADD()DATE_SUB()可以做到:

DATE_SUB(_input_start_date_here_, INTERVAL 30 MINUTE)

Step 2: rethink your problem in terms of start and end times. 步骤2:根据开始时间和结束时间重新考虑您的问题。 Here are the possible cases: 这是可能的情况:

  • if the booking started during the (expanded) period, then you want this booking in your result 如果预订是 (扩展)期间开始的,则您希望此预订包含在结果中
  • or if the booking started before the period, then you want this booking unless it also ended before the period 或者如果预订该时间段之前开始,那么您想要此预订,除非它也在该时间段之前结束
  • on the other hand, if the booking started after the period, then you do not want this booking 另一方面,如果预定该时间段之后开始,则您不希望该预定

The first situation above could be expressed like this: 上面的第一种情况可以这样表示:

WHERE bookingstart_time >= DATE_SUB(_input_start_date_here_, INTERVAL 30 MINUTE)
AND bookingstart_time <= DATE_ADD(_input_end_date_here_, INTERVAL 30 MINUTE)

The second condition is left as an exercise. 第二个条件留作练习。 You can also rewrite the above with a more elegant BETWEEN operator. 您也可以使用更优雅的BETWEEN运算符重写上面的代码。

SELECT restaurant_table FROM rest_restaurantbooking WHERE TIMESTAMPDIFF(SECOND, bookingstart_time, bookingend_time) > 1800. 从rest_restaurantbooking的WHERE TIMESTAMPDIFF(SECOND,bookingstart_time,bookingend_time)中选择1800。

FOR REFERENCE: HERE 参考: 这里

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

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