Fetch date records comparing with the current date’s day and month in MySQL

MySQLMySQLi Database

For this, use MONTH() and DAY(). Let us first create a −

mysql> create table DemoTable1429
   -> (
   -> AnniversaryDate date
   -> );

Insert some records in the table using insert −

mysql> insert into DemoTable1429 values('2019-09-29');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1429 values('2018-09-27');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1429 values('2016-09-28');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1429 values('2015-09-29');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select −

mysql> select * from DemoTable1429;

This will produce the following output −

+-----------------+
| AnniversaryDate |
+-----------------+
| 2019-09-29      |
| 2018-09-27      |
| 2016-09-28      |
| 2015-09-29      |
+-----------------+
4 rows in set (0.00 sec)

The current date is as follows −

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-09-29 |
+------------+
1 row in set (0.00 sec)

Here is the query to fetch date records comparing with current date’s day and month −

mysql> select * from DemoTable1429
   -> where month(AnniversaryDate)=month(curdate())
   -> and day(AnniversaryDate)=day(curdate());

This will produce the following output −

+-----------------+
| AnniversaryDate |
+-----------------+
| 2019-09-29      |
| 2015-09-29      |
+-----------------+
2 rows in set (0.00 sec)
raja
Published on 12-Nov-2019 10:54:36
Advertisements