执行两个日期之间的 MySQL 搜索
要执行两个日期之间的 MySQL 搜索,请使用 BETWEEN 关键字。让我们首先创建一个表 −
mysql> create table DemoTable1456 -> ( -> CustomerName varchar(30), -> StartOfferDate date, -> EndOfferDate date -> ); Query OK, 0 rows affected (0.54 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable1456 values('Chris','2019-09-01','2019-09-30'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1456 values('David','2019-09-01','2019-10-30'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1456 values('Bob','2018-10-01','2018-10-20'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1456;
这将产生以下输出 −
+--------------+----------------+--------------+ | CustomerName | StartOfferDate | EndOfferDate | +--------------+----------------+--------------+ | Chris | 2019-09-01 | 2019-09-30 | | David | 2019-09-01 | 2019-10-30 | | Bob | 2018-10-01 | 2018-10-20 | +--------------+----------------+--------------+ 3 rows in set (0.00 sec)
以下是执行两个日期之间的搜索并获取记录的查询 −
mysql> select * from DemoTable1456 -> where date(now()) between StartOfferDate and EndOfferDate;
这将产生以下输出 −
+--------------+----------------+--------------+ | CustomerName | StartOfferDate | EndOfferDate | +--------------+----------------+--------------+ | David | 2019-09-01 | 2019-10-30 | +--------------+----------------+--------------+ 1 row in set (0.04 sec)
广告