
- 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 to fetch records based on a specific month and year?
For this, use MONTH() and YEAR(). Let us first create a table −
mysql> create table DemoTable1846 ( PurchaseDate date ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1846 values('2019-01-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2019-12-24'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2018-09-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2017-10-26'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1846;
This will produce the following output −
+--------------+ | PurchaseDate | +--------------+ | 2019-01-10 | | 2019-12-24 | | 2018-09-21 | | 2017-10-26 | +--------------+ 4 rows in set (0.00 sec)
Here is the query to fetch records based on a specific month and year
mysql> select * from DemoTable1846 where month(PurchaseDate)=12 and year(PurchaseDate)=2019;
This will produce the following output −
+--------------+ | PurchaseDate | +--------------+ | 2019-12-24 | +--------------+ 1 row in set (0.00 sec)
- Related Articles
- How can fetch records from specific month and year in a MySQL table?
- MySQL SELECT query to return records with specific month and year
- How to sum selected column values based on specific month records in MySQL?
- Do a select in MySQL based only on month and year?
- MySQL query to fetch date with year and month?
- MySQL query to get result by month and year based on condition?
- Selecting data from a MySQL table based on a specific month?
- Fetch ordered records after a specific limit in MySQL
- Fetch records containing a specific character twice in MySQL
- MySQL query for grouping and summing the values based on specific records
- 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
- Display month names and year from a column with date records with MySQL
- MySQL RegExp to fetch records with only a specific number of words
- MySQL REGEXP to fetch string + number records beginning with specific numbers?

Advertisements