 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements
 