
- 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
MySQL select dates in 30-day range?
To select dates in 30-day range, you can use arithmetic operation - with interval.
The syntax is as follows −
select *from yourTableName where yourDateColumnName > NOW() - INTERVAL 30 DAY and yourDateColumnName < NOW() + INTERVAL 30 DAY;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table selectDatesDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ArrivalDate datetime -> ); Query OK, 0 rows affected (0.77 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into selectDatesDemo(ArrivalDate) values('2019-01-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectDatesDemo(ArrivalDate) values('2019-01-29'); Query OK, 1 row affected (0.21 sec) mysql> insert into selectDatesDemo(ArrivalDate) values('2019-02-13'); Query OK, 1 row affected (0.14 sec) mysql> insert into selectDatesDemo(ArrivalDate) values('2019-02-19'); Query OK, 1 row affected (0.14 sec) mysql> insert into selectDatesDemo(ArrivalDate) values('2018-02-13'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectDatesDemo(ArrivalDate) values('2018-03-13'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from selectDatesDemo;
Here is the output −
+----+---------------------+ | Id | ArrivalDate | +----+---------------------+ | 1 | 2019-01-10 00:00:00 | | 2 | 2019-01-29 00:00:00 | | 3 | 2019-02-13 00:00:00 | | 4 | 2019-02-19 00:00:00 | | 5 | 2018-02-13 00:00:00 | | 6 | 2018-03-13 00:00:00 | +----+---------------------+ 6 rows in set (0.00 sec)
Here is the query to select dates in 30-day range −
mysql> select *from selectDatesDemo -> where ArrivalDate > NOW() - INTERVAL 30 DAY -> and ArrivalDate < NOW() + INTERVAL 30 DAY;
The following is The output −
+----+---------------------+ | Id | ArrivalDate | +----+---------------------+ | 3 | 2019-02-13 00:00:00 | | 4 | 2019-02-19 00:00:00 | +----+---------------------+ 2 rows in set (0.04 sec)
- Related Articles
- MySQL query to select all data between range of two dates?
- MySQL Select IN range?
- Select the date records between two dates in MySQL
- SELECT a row by subtracting dates in WHERE in MySQL?
- Select last day of current year in MySQL?
- How to select between/before/after dates in MySQL conditionally?
- MySQL select distinct dates from datetime column in a table?
- How to select month and year from dates in MySQL?
- Select records from MySQL NOW() -1 Day?
- Select specific rows in a range with MySQL?
- Get timestamp date range with MySQL Select?
- MySQL date comparison to fetch dates between a given range?
- How do I generate days from the range of dates in MySQL?
- MySQL - Select dates that are one week ahead from today?
- Python Pandas - Generate dates in a range

Advertisements