Home >Database >Mysql Tutorial >How to Retrieve Data from the Previous Month in MySQL?
Retrieving Data from the Previous Month
This query retrieves all rows from a database that were created in the previous month. The query utilizes the MySQL CURRENT_DATE function to dynamically determine the current month and subtracts one month to target the previous month.
Query Explanation:
SELECT * FROM table WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Breakdown:
WHERE clause filters the results based on the specified conditions:
Example:
If the current date is January 15th, 2023, the query will retrieve all rows from the "table" that were created in December 2022.
The above is the detailed content of How to Retrieve Data from the Previous Month in MySQL?. For more information, please follow other related articles on the PHP Chinese website!