在MySQL中获取日期记录与当前日期的日期和月份进行比较
为此,请使用MONTH()
和DAY()
。让我们首先创建一个-
mysql> create table DemoTable1429 -> ( -> AnniversaryDate date -> );
使用insert在表中插入一些记录-
mysql> insert into DemoTable1429 values('2019-09-29'); mysql> insert into DemoTable1429 values('2018-09-27'); mysql> insert into DemoTable1429 values('2016-09-28'); mysql> insert into DemoTable1429 values('2015-09-29');
使用选择显示表中的所有记录-
mysql> select * from DemoTable1429;
这将产生以下输出-
+-----------------+ | AnniversaryDate | +-----------------+ | 2019-09-29 | | 2018-09-27 | | 2016-09-28 | | 2015-09-29 | +-----------------+ 4 rows in set (0.00 sec)
当前日期如下-
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-09-29 | +------------+ 1 row in set (0.00 sec)
这是查询以获取与当前日期的日期和月份进行比较的日期记录-
mysql> select * from DemoTable1429 -> where month(AnniversaryDate)=month(curdate()) -> and day(AnniversaryDate)=day(curdate());
这将产生以下输出-
+-----------------+ | AnniversaryDate | +-----------------+ | 2019-09-29 | | 2015-09-29 | +-----------------+ 2 rows in set (0.00 sec)