【问题标题】:sqlite find time between two times problem with midnight timesqlite在午夜时间的两次问题之间查找时间
【发布时间】:2020-09-25 23:32:50
【问题描述】:

我在表中有两次 - open_time 和 close time。

Open_time.  Selected time  Close_time
06:00:00    09:23:46       23:45:00
09:00:00    09:23:46       00:00:00
09:00:00    09:23:46       23:00:00
09:00:00    09:23:46       23:00:00
06:00:00    09:23:46       23:45:00
06:00:00    09:23:46       04:00:00
06:00:00    09:23:46       02:00:00

我只需要选择打开和关闭之间的那个时间,但我的表情 有问题。

SELECT open_from, close_at FROM restrnt WHERE (time('now', '+10 hours') BETWEEN  restaurants.open_from and restaurants.close_at)

06:00:00    23:45:00
09:00:00    23:00:00
09:00:00    23:00:00
06:00:00    23:45:00

它只选择 00:00:00 之前的时间,但我需要它在那个片段中找到。

【问题讨论】:

  • 如果您的时间戳包括日期,它可能会容易得多。
  • 这是个好主意,但它喜欢无法存储日期的 CONST 数据。

标签: sqlite datetime time


【解决方案1】:
SELECT time('now','+5 hours') as nowTime, time(restaurants.open_from) as openTime, time(restaurants.close_at) as closeTime,
CASE
    WHEN time(restaurants.open_from) > time(restaurants.close_at) THEN time('now','+5 hours')  >= time(restaurants.open_from) or time('now','+5 hours') < time(restaurants.close_at)
    when time(restaurants.open_from) < time(restaurants.close_at) THEN time('now','+5 hours')  >= time(restaurants.open_from) and time('now','+5 hours')  < time(restaurants.close_at)
    END is_opened
    from restaurants
WHERE is_opened >0

在我的情况下,它可以工作,就像我需要的那样。 输出

06:16:03    06:00:00    23:45:00    1
06:16:03    06:00:00    23:45:00    1
06:16:03    06:00:00    04:00:00    1
06:16:03    06:00:00    02:00:00    1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多