【问题标题】:mysql query with between date with time intervalmysql查询与日期之间的时间间隔
【发布时间】:2018-07-18 08:26:02
【问题描述】:

我有一个带有 created_time 字段的表。 需要找出两个日期之间、特定时间间隔之间的所有条目。 例如,如果我想查找 2018 年 4 月 1 日至 2018 年 4 月 30 日之间时间间隔为 2.30 PM 至 4.30 PM 之间的所有条目,那么理想的查询是什么?

select * from my_table where created_time between '2018-04-01
14:30:00' and '2018-04-30 16:30:00'

【问题讨论】:

  • 此查询将提供这些日期之间的所有条目。我只想要 14.30 到 16.30 之间的条目

标签: mysql date between


【解决方案1】:

您需要拆分比较Date 值和Time 值。

你可以试试这个查询。

TestDLL

CREATE TABLE my_table (
    created_time  DATETIME
);

INSERT INTO my_table VALUES ('2018-04-01 14:00:00');
INSERT INTO my_table VALUES ('2018-04-01 14:50:00');
INSERT INTO my_table VALUES ('2018-04-04 10:00:00');
INSERT INTO my_table VALUES ('2018-04-01 15:00:00');

查询

select * from my_table 
where 
    (created_time between '2018-04-01' and '2018-04-30') 
AND 
    (CAST(created_time AS time) > CAST('14:30:00' AS time) AND CAST(created_time AS time) < CAST('16:30:00' AS time))

[结果]

|         created_time |
|----------------------|
| 2018-04-01T14:50:00Z |
| 2018-04-01T15:00:00Z |

SQLFiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2015-08-10
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 2018-07-26
    相关资源
    最近更新 更多