Home > Database > Mysql Tutorial > How to Select Data from a MySQL Table Between Specific Dates?

How to Select Data from a MySQL Table Between Specific Dates?

Linda Hamilton
Release: 2024-12-09 06:24:09
Original
777 people have browsed it

How to Select Data from a MySQL Table Between Specific Dates?

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

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

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

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!

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