

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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 query to fetch records from a range of months?
Let us first create a table −
mysql> create table DemoTable1795 ( Name varchar(20), DueDate date ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1795 values('John','2018-07-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1795 values('Sam','2019-10-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1795 values('Sam','2019-01-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1795 values('Mike','2018-12-31'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1795;
This will produce the following output −
+------+------------+ | Name | DueDate | +------+------------+ | John | 2018-07-21 | | Sam | 2019-10-21 | | Sam | 2019-01-10 | | Mike | 2018-12-31 | +------+------------+ 4 rows in set (0.00 sec)
Here is the query to get records from a range of months −
mysql> select * from DemoTable1795 where (YEAR(DueDate) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) AND MONTH(DueDate) BETWEEN 7 AND 12) OR (YEAR(DueDate) = YEAR(CURRENT_DATE()) AND MONTH(DueDate) BETWEEN 1 AND 7);
This will produce the following output −
+------+------------+ | Name | DueDate | +------+------------+ | John | 2018-07-21 | | Sam | 2019-01-10 | | Mike | 2018-12-31 | +------+------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- MongoDB query to fetch date records (ISODate format) in a range
- MySQL query to get records after an interval of 8 months
- MySQL query to find a match and fetch records
- MySQL query to fetch the latest date from a table with date records
- MySQL query to fetch records before currentdate + 2 weeks?
- MySQL time period query to fetch date records from interval of 14 weeks from current date?
- MySQL query to fetch records where decimal is a whole number
- MySQL query to fetch specific records matched from an array (comma separated values)
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- How to fetch the newly added records from a MySQL table?
- MySQL query to fetch records wherein timestamp is before 15+ days?
- MySQL query to find alternative records from a table
- How to update a range of records in MySQL?
- MongoDB query to get date records in a range
- MySQL query to display the count of distinct records from a column with duplicate records
Advertisements