
- 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 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 Articles
- MongoDB query to fetch date records (ISODate format) in a range
- 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 get records after an interval of 8 months
- 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 specific records matched from an array (comma separated values)
- MySQL query to fetch records where decimal is a whole number
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- MySQL query to fetch records wherein timestamp is before 15+ days?
- How to fetch the newly added records from a MySQL table?
- MySQL query for text search with LIKE and OR to fetch records
- MySQL query to find alternative records from a table
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to return 5 random records from last 20 records?

Advertisements