在这里,now()
代表当前日期。要检查它是否介于两个特定日期之间,您需要使用BETWEEN。让我们首先创建一个表-
mysql> create table DemoTable ( FirstDate datetime, SecondDate datetime );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-04-01','2019-05-02'); mysql> insert into DemoTable values('2019-05-28','2019-06-04'); mysql> insert into DemoTable values('2016-01-31','2019-03-01');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+-----------------------+-----------------------+ | FirstDate | SecondDate | +-----------------------+-----------------------+ | 2019-04-01 00 :00 :00 | 2019-05-02 00 :00 :00 | | 2019-05-28 00 :00 :00 | 2019-06-04 00 :00 :00 | | 2016-01-31 00 :00 :00 | 2019-03-01 00 :00 :00 | +-----------------------+-----------------------+ 3 rows in set (0.00 sec)
以下是检查now()
两个特定日期之间的瀑布的查询-
mysql> select *from DemoTable where now() BETWEEN FirstDate AND SecondDate;
输出结果
+-----------------------+-----------------------+ | FirstDate | SecondDate | +-----------------------+-----------------------+ | 2019-05-28 00 :00 :00 | 2019-06-04 00 :00 :00 | +-----------------------+-----------------------+ 1 row in set (0.00 sec)