Home >Database >Mysql Tutorial >How to Select Data from the Beginning of the Current Month to Today in MySQL?
Selecting Data Between the Beginning and End of the Current Month
To retrieve data within a specific date range, MySQL provides several date functions. This article addresses a specific scenario: selecting data from the first day of the current month to the current day.
Query Structure
The general query structure for selecting data between a date range is:
SELECT * FROM table_name WHERE date BETWEEN start_date AND end_date
Start and End Dates
To determine the start and end dates for the current month, we can utilize MySQL's DATE_SUB(), LAST_DAY(), and CURDATE() functions.
Example Query
Using DATE_ADD() to add one day to the last day of the previous month:
SELECT * FROM table_name WHERE date BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 30 DAY), INTERVAL 1 DAY) AND CURDATE()
Simplified Version
Alternatively, a simplified version of the query using DATE_FORMAT() to extract the first day of the current month:
SELECT * FROM table_name WHERE date BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND NOW()
The above is the detailed content of How to Select 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!