Home > Database > Mysql Tutorial > How to Select Data in MySQL Between the First Day of the Current Month and Today?

How to Select Data in MySQL Between the First Day of the Current Month and Today?

Patricia Arquette
Release: 2024-11-28 00:23:10
Original
497 people have browsed it

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"
Copy after login

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() )
Copy after login

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() )
Copy after login

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!

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