MySQL SELECT last few days?

MySQLMySQLi Database

To select last few days, use DATE_ADD() function in MySQL. The syntax is as follows −

select date_add(curdate(),interval - anyIntgegerValue day);

Or you can DATE_SUB() from MySQL.

select date_sub(curdate(),interval anyIntgegerValue day);

Or you can use the following syntax −

select curdate() - interval anyIntgegerValue day;

Here is the example of all syntaxes shown above to select last few days.

Case 1 − Use of DATE_ADD() function

The query is as follows −

mysql> select date_add(curdate(),interval -6 day);

Here is the output −

+-------------------------------------+
| date_add(curdate(),interval -6 day) |
+-------------------------------------+
| 2018-11-20                          |
+-------------------------------------+
1 row in set (0.00 sec)

Case 2 − Use of DATE_SUB() function

The query is as follows −

mysql> select date_sub(curdate(),interval 6 day);

Here is the output −

+------------------------------------+
| date_sub(curdate(),interval 6 day) |
+------------------------------------+
| 2018-11-20                         |
+------------------------------------+
1 row in set (0.00 sec)

Case 3 − Use of minus (-) symbol

The query is as follows −

mysql> select curdate()-interval 6 day;

Here is the output −

+--------------------------+
| curdate()-interval 6 day |
+--------------------------+
| 2018-11-20               |
+--------------------------+
1 row in set (0.00 sec)
raja
Published on 24-Dec-2018 11:31:53
Advertisements