3

我有一个表有两个字段 dt(DateTime) 和 isblocked(Bit)

在此处输入图像描述

我一直在尝试做的是找到两个日期/时间之间的时间跨度,其中有 45 分钟连续免费 ​​- 基于开始日期/时间。

例如:根据上表使用 2012-12-11 13:30:00' 作为我的开始日期。我需要这样的值:

2012-12-11 15:30:00
2012-12-11 16:45:00

我尝试了一些最小和最大查询,例如:

SELECT MIN(dt), isbooked
FROM booking
WHERE dt >= '2012-12-11 13:30:00' AND isbooked = 1
GROUP BY dt
LIMIT 1;

但我似乎无法将正确的结构放在一起。如果需要更多信息,请告诉我。

4

4 回答 4

2

在 MySQL 中,您可以使用相关子查询来执行此操作:

select min(b.dt) as StartTime, NotBookedTime as EndTime
from (select b.*,
             (select min(b2.dt) from booking b2 where b2.dt >= b.dt and b2.isbooked = 1
             ) as NotBookedTime
      from booking b
     ) b
where b.isbooked = 0
group by NotBookedTime
having date_add(min(b.dt), interval 45 minute) <=  NotBookedTime

它的作用是找到每条记录的第一个预定间隔,并记住时间。然后它会找到第一个预定的间隔,并检查差异何时为 45 分钟或更大。

于 2012-12-11T20:52:31.480 回答
1

这也应该有效:

SELECT d.dt
FROM (
  SELECT
    t1.dt,
    MIN(case when t2.isBooked then t2.dt end) AS next_booked,
    MAX(case when not t2.isBooked then t2.dt end) as max_unbooked
  FROM times t1 INNER JOIN times t2 ON t1.dt < t2.dt
  WHERE t1.isBooked =0
  GROUP BY t1.dt
) d
WHERE
  timediff(next_booked,d.dt)>='00:45:00' or
  (next_booked is null and timediff(max_unbooked,d.dt)>='00:30:00')

取决于你所追求的,它也可以像这样简化:

SELECT d.dt
FROM (
  SELECT
    t1.dt,
    MIN(t2.dt) as next_booked
  FROM times t1 left  JOIN times t2 ON t1.dt < t2.dt and t2.isBooked=1
  WHERE t1.isBooked =0
  GROUP BY t1.dt
) d
WHERE timediff(d.next_booked, d.dt)>='00:45:00'
      or d.next_booked is null
GROUP BY d.next_booked
于 2012-12-11T21:00:48.123 回答
0

看看这个SQLFiddle

SELECT min(b.dt) AS StartTime, NotBookedTime AS EndTime
FROM (SELECT b.*,
        (SELECT min(b2.dt) FROM DT b2 
           WHERE b2.dt >= b.dt AND b2.isBooked = 1
         ) AS NotBookedTime
     FROM DT b
    ) b
WHERE b.isBooked = 0
GROUP BY NotBookedTime
HAVING EndTime is not null
于 2012-12-11T21:01:38.653 回答
0

您可以使用变量:

set @tp=now();
SELECT @tp:=dt as dt,isbooked FROM booking WHERE 
    dt >= addtime(@tp,'00:45:00') AND isbooked = 1
    ORDER BY dt;

@TP它将采用上一个所选日期的值。

于 2012-12-11T21:08:31.923 回答