在MySQL中按当前日期过滤查询

让我们首先创建一个表-

mysql> create table DemoTable(DueDate datetime);

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values('2019-07-10 04:20:00');
mysql> insert into DemoTable values('2019-07-10 05:10:40');
mysql> insert into DemoTable values('2019-07-10 09:00:20');
mysql> insert into DemoTable values('2019-07-10 10:01:04');
mysql> insert into DemoTable values('2019-07-10 12:11:10');

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;

这将产生以下输出-

+---------------------+
| DueDate             |
+---------------------+
| 2019-07-10 04:20:00 |
| 2019-07-10 05:10:40 |
| 2019-07-10 09:00:20 |
| 2019-07-10 10:01:04 |
| 2019-07-10 12:11:10 |
+---------------------+
5 rows in set (0.00 sec)

以下是按当前日期过滤的查询-

mysql> select *from DemoTable where date(DueDate)=curdate()
   and time(DueDate) > '04:00:00'
   and time(DueDate) < '10:00:00';

这将产生以下输出-

+---------------------+
| DueDate             |
+---------------------+
| 2019-07-10 04:20:00 |
| 2019-07-10 05:10:40 |
| 2019-07-10 09:00:20 |
+---------------------+
3 rows in set (0.00 sec)