
- 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
How can fetch records from specific month and year in a MySQL table?
Use YEAR() and MONTH() to display records from specific month and year respectively. Let us first create a table −
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(20), CustomerTotalBill int, PurchasingDate date ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(CustomerName,CustomerTotalBill,PurchasingDate) values('John',2000,'2019-01-21'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(CustomerName,CustomerTotalBill,PurchasingDate) values('Chris',1000,'2019-01-31'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(CustomerName,CustomerTotalBill,PurchasingDate) values('Robert',4500,'2018-01-01'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(CustomerName,CustomerTotalBill,PurchasingDate) values('Sam',5500,'2017-02-12'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(CustomerName,CustomerTotalBill,PurchasingDate) values('Carol',500,'2016-01-12'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+--------------+-------------------+----------------+ | CustomerId | CustomerName | CustomerTotalBill | PurchasingDate | +------------+--------------+-------------------+----------------+ | 1 | John | 2000 | 2019-01-21 | | 2 | Chris | 1000 | 2019-01-31 | | 3 | Robert | 4500 | 2018-01-01 | | 4 | Sam | 5500 | 2017-02-12 | | 5 | Carol | 500 | 2016-01-12 | +------------+--------------+-------------------+----------------+ 5 rows in set (0.00 sec)
Following is the query to display records from specific month and year in MySQL −
mysql> select *from DemoTable WHERE YEAR(DATE(PurchasingDate))=2019 AND MONTH(DATE(PurchasingDate)) = 01;
This will produce the following output −
+------------+--------------+-------------------+----------------+ | CustomerId | CustomerName | CustomerTotalBill | PurchasingDate | +------------+--------------+-------------------+----------------+ | 1 | John | 2000 | 2019-01-21 | | 2 | Chris | 1000 | 2019-01-31 | +------------+--------------+-------------------+----------------+ 2 rows in set (0.03 sec)
- Related Questions & Answers
- MySQL to fetch records based on a specific month and year?
- MySQL SELECT query to return records with specific month and year
- How can we fetch all the records from a particular MySQL table?
- How can we fetch alternate even-numbered records from MySQL table?
- How can we fetch alternate odd numbered records from MySQL table?
- MySQL query to fetch date with year and month?
- Display month names and year from a column with date records with MySQL
- How can we extract the Year and Month from a date in MySQL?
- How can we fetch month and day from a given date in MySQL?
- How to fetch the newly added records from a MySQL table?
- Fetch month, day, year, etc. from ISODate in MongoDB?
- Filter the records of current day, month and year in MySQL?
- How to compare Year, Month and Day in a MySQL query and display matching records
- How to select month and year from dates in MySQL?
- Selecting data from a MySQL table based on a specific month?
Advertisements