"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Query Database Rows from the Previous Month?

How to Query Database Rows from the Previous Month?

Posted on 2025-02-25
Browse:257

How to Query Database Rows from the Previous Month?

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)

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.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3