- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL SELECT last few days?
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)
Advertisements