Selecting MySQL Data Between Two Dates
Selecting a range of data from a MySQL table based on dates can be achieved using the BETWEEN operator. Let's explore how to retrieve data from a specific date range.
For a range spanning from January 1, 2009, to the current date, use the following query:
select * from table_name where datetime_column BETWEEN '01/01/2009' and curdate()
If you intend to include the current date as well, you can use the Greater Than or Equal To (>=) and Less Than or Equal To (<=) operators instead:
select * from table_name where datetime_column >= '01/01/2009' and datetime_column <= curdate()</p> <p><strong>Count of Daily Data</strong></p> <p>To count or aggregate daily data, you can combine the BETWEEN operator with the COUNT() function. For instance, to get a daily count from January 1, 2009, use this query:</p> <pre class="brush:php;toolbar:false">select count(*) from table_name where datetime_column BETWEEN '01/01/2009' and DATE_ADD('01/01/2009', INTERVAL 1 DAY)
Replace the example date '01/01/2009' with your desired starting date. This query will provide a count for every day in the specified range.
The above is the detailed content of How to Select and Count MySQL Data Within a Specific Date Range?. For more information, please follow other related articles on the PHP Chinese website!