在一个表中选择的时间与使用 MySQL 的不同表中的时间相差超过一定时间的时间
在 MySQL 中,如何查询一个表中所有时间的给定间隔之外的时间?
在下面的示例表中,如果我想选择表 A 中与表 B 中的时间相差两个小时或更长时间的所有内容,是否可以?
澄清:就我而言,可以忽略/忽略夏令时转换、时区和午夜的日期换行。
示例表:
表 A
times
00:00:00
00:30:00
01:00:00
01:30:00
02:00:00
02:30:00
03:00:00
03:30:00
04:00:00
04:30:00
.... all the other times in the day on 30 minute increments
22:00:00
22:30:00
23:00:00
23:30:00
表 B
11:00:00
15:30:00
期望的结果:
00:00:00
to
09:00:00
13:00:00
13:30:00
17:30:00
to
23:30:00
已更新,正在运行的查询:
set @dayToCheck = '2024-04-01';
set @earliest = '09:00:00';
set @latest = '17:00:00';
set @breakStart = '00:00:00';
set @breakDuration = 0;
set @inc = 59;
select t.times from times t
#the time isn't taken
where t.times not in (select a.time from aptSchedule a where a.date = @dayToCheck)
#the time is within the regular schedule
and t.times >= @earliest
and t.times <= @latest
#the time is not during a break
and t.times not in (select s.times from times s where s.times >= @breakStart and times < DATE_ADD(concat(@dayToCheck," ", @breakStart), INTERVAL @breakDuration HOUR))
#the time is outside of the allotted appointment increment
and not exists
(select 1 from
(select time from aptSchedule b where date = @dayToCheck) dr
where abs(time_to_sec(timediff(t.times,dr.time))) <= @inc*60);