MySQL查询以查找日期记录,其中当前日期和时间在JoiningDate和RelievingDate之间
使用BETWEEN查找加入和放宽日期之间的日期和时间。NOW()
用于获取当前日期和时间进行比较。
让我们首先创建一个表-
mysql> create table DemoTable771 ( Joiningdate datetime, Relievingdate datetime );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable771 values('2016-01-21','2016-09-23'); mysql> insert into DemoTable771 values('2019-01-21','2019-09-23'); mysql> insert into DemoTable771 values('2017-04-01','2018-12-31'); mysql> insert into DemoTable771 values('2019-04-01','2019-12-31');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable771;
这将产生以下输出-
+---------------------+---------------------+ | Joiningdate | Relievingdate | +---------------------+---------------------+ | 2016-01-21 00:00:00 | 2016-09-23 00:00:00 | | 2019-01-21 00:00:00 | 2019-09-23 00:00:00 | | 2017-04-01 00:00:00 | 2018-12-31 00:00:00 | | 2019-04-01 00:00:00 | 2019-12-31 00:00:00 | +---------------------+---------------------+ 4 rows in set (0.00 sec)
以下是用于查询日期记录的MySQL查询,其中当前日期和时间在JoiningDate和RelievingDate之间-
mysql> select *from DemoTable771 where NOW() between Joiningdate and Relievingdate;
这将产生以下输出-
+---------------------+---------------------+ | Joiningdate | Relievingdate | +---------------------+---------------------+ | 2019-01-21 00:00:00 | 2019-09-23 00:00:00 | | 2019-04-01 00:00:00 | 2019-12-31 00:00:00 | +---------------------+---------------------+ 2 rows in set (0.07 sec)