Home > Database > Mysql Tutorial > How to Query Database Rows from the Previous Month?

How to Query Database Rows from the Previous Month?

Linda Hamilton
Release: 2024-11-30 16:48:11
Original
629 people have browsed it

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)
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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template