Home > Database > Mysql Tutorial > How to Select MySQL Data Between Two Dates?

How to Select MySQL Data Between Two Dates?

Susan Sarandon
Release: 2024-12-16 02:04:10
Original
645 people have browsed it

How to Select MySQL Data Between Two Dates?

Selecting Data from MySQL Between Two Dates

To select data from a MySQL table between a specified date and the current date, you can use the BETWEEN operator in combination with the DATE function. The BETWEEN operator is used to compare a value to a range of values. The DATE function is used to extract the date component from a datetime value.

For example, to select all data from a table called "orders" where the "order_date" column is between January 1, 2009, and the current date, you would use the following query:

SELECT * FROM orders WHERE order_date BETWEEN '2009-01-01' AND CURRENT_DATE();
Copy after login

You can also use the >= and <= operators to achieve the same result:

SELECT * FROM orders WHERE order_date >= '2009-01-01' AND order_date <= CURRENT_DATE();</p>
<p><strong>Selecting Daily Data</strong></p>
<p>If you want to get daily data for a specific period, you can use the GROUP BY clause and the DATE function. The GROUP BY clause is used to group the results of a query by one or more columns. The DATE function is used to extract the date component from a datetime value.</p>
<p>For example, to get daily counts of orders from the "orders" table for the period between January 1, 2009, and the current date, you would use the following query:</p>
<pre class="brush:php;toolbar:false">SELECT DATE(order_date) AS order_date, COUNT(*) AS order_count FROM orders WHERE order_date BETWEEN '2009-01-01' AND CURRENT_DATE() GROUP BY order_date;
Copy after login

This query will return the number of orders for each day in the specified period.

The above is the detailed content of How to Select MySQL Data Between Two Dates?. 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