SQL 查询在 where 条件下的某些时间不工作

SQL Query not working with certain times in where condition

我在 DB2 中创建了一个 table。这个 table 有 4 个字段 ID(Integer)、Shift(Integer)、Start_time(Time) 和 End_time(Time)。此 table 中的数据看起来像 this。当我运行这个select * from shifttimes where '6:29:59' between start_time and End_timeSQL查询时,它returns“1”作为班次编号。早上 5 点到 1:29PM 之间的任何时间,它 returns “1” 作为班次编号,这很好。

当我运行这个select * from shifttimes where '20:29:59' between start_time and End_timesql查询时,它returns“2”作为班次编号。在1:30PM到8:29:59PM之间的任何时间,它returns“2”作为班次号码,这也很好。

问题是当我 运行 这个 select * from shifttimes where '20:30:00' between start_time and End_time sql 查询时,它 returns 什么也没有。 从 20:30:00 到 4:59:00 的任何时间,查询 returns 什么都没有。 我在 TOAD 中为 DB2 执行所有这些查询。

你想要的逻辑是:

select *
from shifttimes
where (start_time < end_time and '20:29:59' between start_time and End_time) or
      (start_time > end_time and '20:29:59 not between end_time and start_time)

问题(您可能已经注意到)是超过午夜边界的班次。第二个条件处理。