Home > Database > Mysql Tutorial > How to Retrieve MySQL Records Within a Specific Date Range?

How to Retrieve MySQL Records Within a Specific Date Range?

DDD
Release: 2024-12-02 07:55:11
Original
1017 people have browsed it

How to Retrieve MySQL Records Within a Specific Date Range?

Retrieve Records within a Date Range in MySQL

To select data from a MySQL table between a specified date and the current date, utilize the BETWEEN operator. The syntax is as follows:

SELECT *
FROM table_name
WHERE datetime_column BETWEEN 'start_date' AND CURDATE()
Copy after login

For instance, to retrieve data from January 1, 2009, to the current date, execute the following query:

SELECT *
FROM table_name
WHERE datetime_column BETWEEN '2009-01-01' AND CURDATE()
Copy after login

Alternatively, you can use the >= and <= operators:

SELECT *
FROM table_name
WHERE datetime_column >= '2009-01-01' AND datetime_column <= CURDATE()</p>
<p><strong>Retrieving Day-to-Day Data</strong></p>
<p>If you wish to obtain day-to-day data from January 1, 2009, you can employ a combination of COUNT() and BETWEEN:</p>
<pre class="brush:php;toolbar:false">SELECT DATE(datetime_column) AS day, COUNT(*) AS count
FROM table_name
WHERE datetime_column BETWEEN '2009-01-01' AND CURDATE()
GROUP BY day
Copy after login

This query will group the results by day and provide the count of records for each day within the specified date range.

The above is the detailed content of How to Retrieve MySQL Records Within a Specific Date Range?. 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