使用 MySQL 中的当前日期筛选查询
让我们首先创建一个表 -
mysql> create table DemoTable(DueDate datetime); Query OK, 0 rows affected (0.94 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values('2019-07-10 04:20:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-07-10 05:10:40'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-07-10 09:00:20'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2019-07-10 10:01:04'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2019-07-10 12:11:10'); Query OK, 1 row affected (0.19 sec)
使用 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)
广告