Home > Database > Mysql Tutorial > How to Retrieve Data from the Beginning of the Current Month to Today in MySQL?

How to Retrieve Data from the Beginning of the Current Month to Today in MySQL?

Susan Sarandon
Release: 2024-12-08 09:47:15
Original
439 people have browsed it

How to Retrieve Data from the Beginning of the Current Month to Today in MySQL?

Retrieving Data Between Current Month's First Day and Today in MySQL

To obtain data within a specific timeframe, it is necessary to define the boundaries accurately. When seeking records between the first day of the current month and the current day in MySQL, the following query can be employed:

SELECT *
FROM table_name
WHERE date BETWEEN '1st day of current month' AND 'current day';
Copy after login

Example Query:

However, directly specifying the dates as strings in the query can be cumbersome and error-prone. To enhance flexibility, consider the following working example:

SELECT *
FROM table_name
WHERE (date BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 30 DAY)), INTERVAL 1 DAY) AND CURDATE());
Copy after login

This query utilizes date manipulation functions to calculate the first day of the current month and combine it with the current date.

Alternative Query:

Another convenient approach to achieve this selection is:

SELECT *
FROM table_name
WHERE (date BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND NOW());
Copy after login

This query directly formats the current date into a string representing the first day of the current month and combines it with the current date for comparison in the WHERE clause.

These queries provide a flexible and dynamic way to retrieve data within the desired date range in MySQL, simplifying data extraction and analysis.

The above is the detailed content of How to Retrieve Data from the Beginning of the Current Month to Today in MySQL?. 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