Home >Database >Mysql Tutorial >How to Select Data from the 1st of the Current Month to Today in MySQL?
Selecting Between the 1st Day and Current Day in MySQL
When working with MySQL, it can be useful to query data for a specific range of dates. One common scenario is to select data from the 1st day of the current month to the present day.
To accomplish this, you can use the BETWEEN operator along with appropriate date functions. Here's an example query:
SELECT * FROM table_name WHERE date BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 30 DAY)), INTERVAL 1 DAY) AND CURDATE()
This query uses the LAST_DAY function to subtract 30 days from the current date and find the last day of the previous month. DATE_ADD is then used to add 1 day to find the 1st day of the current month. CURDATE provides the current date.
Improved Query:
Another simplified and efficient approach is:
SELECT * FROM table_name WHERE date BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND NOW()
This query uses the DATE_FORMAT function to extract only the year, month, and day. By concatenating it with "-01", it effectively sets the date to the 1st day of the current month. NOW() returns the current date.
Both queries will return data rows that fall within the specified date range, allowing you to perform your analysis or reporting tasks effectively.
The above is the detailed content of How to Select Data from the 1st of the Current Month to Today in MySQL?. For more information, please follow other related articles on the PHP Chinese website!