How to select a date less than the current date with MySQL?


Let us first create a table −

mysql> create table DemoTable1877
   (
   DueDate datetime
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1877 values('2019-12-10');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1877 values('2019-12-05');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1877 values('2019-12-07');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1877 values('2019-12-09');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1877;

This will produce the following output −

+---------------------+
| DueDate             |
+---------------------+
| 2019-12-10 00:00:00 |
| 2019-12-05 00:00:00 |
| 2019-12-07 00:00:00 |
| 2019-12-09 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

The current date is as follows −

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

Here is the query to select a date less than the current one −

mysql> select * from DemoTable1877 where DueDate < now();

This will produce the following output −

+---------------------+
| DueDate             |
+---------------------+
| 2019-12-05 00:00:00 |
| 2019-12-07 00:00:00 |
+---------------------+
2 rows in set (0.00 sec)

Updated on: 27-Dec-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements