Languages
[Edit]
EN

MySQL - select between two dates

0 points
Created by:
Teilsa
1255

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:

MySQL - example data used to select between two dates
MySQL - example data used to select between two dates

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:

MySQL - select between two dates - result
MySQL - select between two dates - result

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');
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - dates

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join