먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable ( FirstDate datetime, SecondDate datetime ); Query OK, 0 rows affected (0.57 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable values('2019-01-21','2018-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-10-04','2019-08-14'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-05-01','2019-09-11'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable values(NULL,NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-03-01',NULL); Query OK, 1 row affected (0.13 sec)
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)