
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to query between two dates in MySQL?
You can query between dates with the help of BETWEEN statement. The syntax is as follows −
select *from yourTableName where yourColumnName between ‘yourStartingDate’ and curdate().
Use curdate() or now(), both these functions will work. To understand the above syntax, let us create a table −
mysql> create table BetweenDateDemo −> ( −> StartDate datetime −> ); Query OK, 0 rows affected (0.78 sec)
Insert some records in the table with the help of the following query −
mysql> insert into BetweenDateDemo values(date_add(now(),interval -1 year)); Query OK, 1 row affected (0.11 sec) mysql> insert into BetweenDateDemo values(date_add(now(),interval -2 year)); Query OK, 1 row affected (0.13 sec) mysql> insert into BetweenDateDemo values(date_add(now(),interval -3 year)); Query OK, 1 row affected (0.13 sec) mysql> insert into BetweenDateDemo values(date_add(now(),interval 1 year)); Query OK, 1 row affected (0.12 sec) mysql> insert into BetweenDateDemo values(date_add(now(),interval 2 year)); Query OK, 1 row affected (0.12 sec) mysql> insert into BetweenDateDemo values(date_add(now(),interval 3 year)); Query OK, 1 row affected (0.16 sec)
Now you can display all records from the table with the help of select statement. The query is as follows −
mysql> select *from BetweenDateDemo;
The following is the output −
+---------------------+ | StartDate | +---------------------+ | 2017-12-08 11:45:47 | | 2016-12-08 11:45:56 | | 2015-12-08 11:46:01 | | 2019-12-08 11:46:05 | | 2020-12-08 11:46:11 | | 2021-12-08 11:46:15 | +---------------------+ 6 rows in set (0.00 sec)
Now perform select between dates using the syntax discussed above −
mysql> select *from BetweenDateDemo where StartDate between '2014-8-12' and curdate();
The following is the output −
+---------------------+ | StartDate | +---------------------+ | 2017-12-08 11:45:47 | | 2016-12-08 11:45:56 | | 2015-12-08 11:46:01 | +---------------------+ 3 rows in set (0.00 sec)
Use ORDER BY to get in a sorted order. The query is as follows −
mysql> select *from BetweenDateDemo where StartDate between '2014-8-12' and curdate() order by StartDate;
The following is the output −
+---------------------+ | StartDate | +---------------------+ | 2015-12-08 11:46:01 | | 2016-12-08 11:45:56 | | 2017-12-08 11:45:47 | +---------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to select all data between range of two dates?
- Perform MySQL search between two dates
- MySQL query to calculate the days between two dates from different columns but similar rows
- How to list all dates between two dates in Excel?
- How to check whether now() falls between two specific dates in MySQL?
- How to create a vector with dates between two dates in R?
- Select the date records between two dates in MySQL
- How to count days between two dates in Java
- How to search date between two dates in MongoDB?
- How to calculate average between two dates in Excel?
- How to calculate minutes between two dates in JavaScript?
- MySQL query to count the dates and fetch repeated dates as well
- Find the number of logins between two dates in MySQL
- MySQL query to order and display difference between dates from the current date
- How can I calculate full 24hour days between two specified dates in MySQL?

Advertisements