要在两个日期之间执行MySQL搜索,请使用BETWEEN关键字。让我们首先创建一个表-
create table DemoTable1456 -> ( -> CustomerName varchar(30), -> StartOfferDate date, -> EndOfferDate date -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1456 values('Chris','2019-09-01','2019-09-30'); insert into DemoTable1456 values('David','2019-09-01','2019-10-30'); insert into DemoTable1456 values('Bob','2018-10-01','2018-10-20');
使用select语句显示表中的所有记录-
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)
以下是在两个日期之间执行搜索并获取记录的查询-
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)