Querying Database Rows from the Previous Month
To retrieve all rows from a database that were created within the preceding month, consider the following query:
SELECT *
FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Copy after login
Let's break down this query:
-
SELECT * FROM table: This retrieves all columns and rows from the specified table.
-
YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH): This condition filters the rows based on the year of the date_created column. It ensures that the rows match the year of the current date minus one month.
-
MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH): Similarly, this condition filters the rows based on the month of the date_created column. It ensures that the rows match the month of the current date minus one month.
The above is the detailed content of How to Query Database Rows from the Previous Month?. For more information, please follow other related articles on the PHP Chinese website!