EN
MySQL - select between two dates
0
points
In this article, we would like to show you how to select between two dates in MySQL.
Quick solution:
SELECT * FROM `table_name`
WHERE `column_name`
BETWEEN 'yyyy-mm-dd' AND 'yyyy-mm-dd';
Practical example
To show how to select between two dates, we will use the following table:
Note:
At the end of this article you can find database preparation SQL queries.
Example
In this example, we will select all the dates from events
table between 2000-01-05
and 2000-01-08
.
Query:
SELECT * FROM `events`
WHERE (`event_date` BETWEEN '2021-01-05' AND '2021-01-08');
Output:
Database preparation
create_tables.sql
file:
CREATE TABLE `events`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`event_date` DATE,
PRIMARY KEY (`id`)
);
insert_data.sql
file:
INSERT INTO `events`
(`event_date`)
VALUES
('2021-01-01'),
('2021-01-02'),
('2021-01-03'),
('2021-01-04'),
('2021-01-05'),
('2021-01-06'),
('2021-01-07'),
('2021-01-08'),
('2021-01-09'),
('2021-01-10');