Selecting Data from MySQL Table Between Dates
Selecting data from a MySQL table based on a range of dates is a common task in database programming. This task can be accomplished using the BETWEEN operator.
To select data from a table for a specific date range, use the following syntax:
SELECT * FROM table_name WHERE datetime_column BETWEEN 'start_date' AND 'end_date'
For example, to select data from January 1, 2009 to the current date, use the following query:
SELECT * FROM table_name WHERE datetime_column BETWEEN '2009-01-01' AND CURDATE()
To select data for a specific range of days, such as day by day from January 1, 2009, use the COUNT() function along with the BETWEEN operator:
SELECT COUNT(*) FROM table_name WHERE datetime_column BETWEEN '2009-01-01' AND '2009-01-02'
This query will return the number of rows in the table that have a datetime_column value between January 1, 2009 and January 2, 2009.
The above is the detailed content of How to Select Data from a MySQL Table Between Specific Dates?. For more information, please follow other related articles on the PHP Chinese website!