Home >Database >Mysql Tutorial >How to Select Data in MySQL Between the First Day of the Current Month and Today?
Selecting Between Current Month's First Day and Current Day in MySQL
Question:
How do you retrieve data from a MySQL database within a specific timeframe, from the first day of the current month to the current day?
Example Query:
The following query attempts to select data from a table between the first day of the current month and the current day:
select*from table_name where date between "1st day of current month" and "current day"
This query, however, will result in an error as it uses non-standard syntax.
Working Example:
Here are two working examples of queries that correctly select data within the specified timeframe:
select * from table_name where (date between DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), interval 30 day), interval 1 day) AND CURDATE() )
This query retrieves data from dates falling between the first day of the previous month and the current day inclusive.
select * from table_name where (date between DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
This query provides a more concise alternative by directly specifying the first day of the current month using DATE_FORMAT(NOW() ,'%Y-%m-01'). Both queries return data within the desired timeframe.
The above is the detailed content of How to Select Data in MySQL Between the First Day of the Current Month and Today?. For more information, please follow other related articles on the PHP Chinese website!