Home >Database >Mysql Tutorial >How to Select Rows from the Previous Month in MySQL?
Selecting Rows from the Previous Month
Database queries often require retrieving data based on specific criteria, such as selecting rows that belong to a particular time period. For instance, you may need to fetch all records within the current month or those from the previous month. Let's explore a query to accomplish the latter.
Querying for Last Month's Rows
To retrieve rows created in the previous month, the following MySQL query can be utilized:
SELECT * FROM table WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Understanding the Query:
By utilizing this query, you can effectively select rows that were created in the previous month, regardless of the current month. This versatile approach is useful for various data analysis and reporting tasks.
The above is the detailed content of How to Select Rows from the Previous Month in MySQL?. For more information, please follow other related articles on the PHP Chinese website!