Home > Database > Mysql Tutorial > body text

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

DDD
Release: 2024-11-27 05:53:13
Original
764 people have browsed it

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

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

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

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template