使用 MySQL 查询检查当前日期是否在给定的日期范围内
我们先创建一个表 −
mysql> create table DemoTable1448 -> ( -> StartDate date, -> EndDate date -> ); Query OK, 0 rows affected (0.46 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable1448 values('2019-01-21','2019-03-22'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1448 values('2019-04-05','2019-10-10'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1448 values('2019-10-01','2019-10-29'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1448 values('2018-12-31','2019-12-31'); Query OK, 1 row affected (0.12 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1448;
将会生成以下输出 −
+------------+------------+ | StartDate | EndDate | +------------+------------+ | 2019-01-21 | 2019-03-22 | | 2019-04-05 | 2019-10-10 | | 2019-10-01 | 2019-10-29 | | 2018-12-31 | 2019-12-31 | +------------+------------+ 4 rows in set (0.00 sec)
假设当前日期为 −
2019-10-05
以下是查询当前日期是否在给定日期范围内的查询 −
mysql> select (curdate() >=StartDate and curdate() <=EndDate) as DateInRange from DemoTable1448;
将会生成以下输出 −
+-------------+ | DateInRange | +-------------+ | 0 | | 1 | | 1 | | 1 | +-------------+ 4 rows in set (0.04 sec)
广告