让我们首先创建一个表-
mysql> create table DemoTable ( FirstDate datetime, SecondDate datetime );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-01-21','2018-01-21'); mysql> insert into DemoTable values('2019-10-04','2019-08-14'); mysql> insert into DemoTable values('2019-05-01','2019-09-11'); mysql> insert into DemoTable values(NULL,NULL); mysql> insert into DemoTable values('2019-03-01',NULL);
使用select语句显示表中的所有记录-
Display all records from the table using select statement:
这将产生以下输出-
+---------------------+---------------------+ | FirstDate | SecondDate | +---------------------+---------------------+ | 2019-01-21 00:00:00 | 2018-01-21 00:00:00 | | 2019-10-04 00:00:00 | 2019-08-14 00:00:00 | | 2019-05-01 00:00:00 | 2019-09-11 00:00:00 | | NULL | NULL | | 2019-03-01 00:00:00 | NULL | +---------------------+---------------------+ 5 rows in set (0.00 sec)
以下是从具有DATE和NULL记录的表中过滤日期的查询-
mysql> select *from DemoTable where FirstDate <= curdate() and (SecondDate >=curdate() or SecondDate IS NULL);
这将产生以下输出-
+---------------------+---------------------+ | FirstDate | SecondDate | +---------------------+---------------------+ | 2019-05-01 00:00:00 | 2019-09-11 00:00:00 | | 2019-03-01 00:00:00 | NULL | +---------------------+---------------------+ 2 rows in set (0.00 sec)