如何在sqlite中选择从今天中午12点到次日凌晨5点的销售

时间:2018-11-08 07:38:19

标签: sqlite

我需要选择从今天中午12点到次日凌晨5点的每日销售额。

示例:2018-11-08 12:002018-11-09 05:00(于2018-11-08启动)

我能够在23:59之前检索每日销售额,但无法获取第二天的05:00。

我有Orders表,其数据类型为Order_date,日期时间格式为TEXT

1 个答案:

答案 0 :(得分:0)

我相信可以使用以下内容:-

SELECT * FROM orders 
  WHERE order_date 
    BETWEEN (strftime('%Y-%m-%d','now')||' 12:00') 
    AND (strftime('%Y-%m-%d','now','+1 days')||' 05:00')
;

工作示例

DROP TABLE IF EXISTS orders;
CREATE TABLE IF NOT EXISTS orders (order_date TEXT);
INSERT INTO orders (order_date) VALUES
        (strftime('%Y-%m-%d','now','-1 days')||' 11:59'), -- before
        (strftime('%Y-%m-%d','now')||' 00:00'), -- before
        (strftime('%Y-%m-%d','now')||' 11:59'), -- before (just)
        (strftime('%Y-%m-%d','now')||' 12:00'), --*** included
        (strftime('%Y-%m-%d','now')||' 23:59'), --*** included
        (strftime('%Y-%m-%d','now','+1 days')||' 05:00'), --**** included
        (strftime('%Y-%m-%d','now','+1 days')||' 05:01') -- after (just)
    ;

SELECT * FROM orders 
 WHERE order_date 
   BETWEEN (strftime('%Y-%m-%d','now')||' 12:00') 
   AND (strftime('%Y-%m-%d','now','+1 days')||' 05:00')
;
  • 标有***的是应选择的行(其中3行)。 enter image description here
相关问题