
- 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
Do a select in MySQL based only on month and year?
To select MySQL based on month and year, use in-built function YEAR() and MONTH(). The syntax is as follows −
select *from yourTableName where YEAR(yourColumnName) = YearValue AND MONTH(yourColumnName) = monthValue;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table selectDataOnYearandMonthDemo −> ( −> BookId int, −> BookName varchar(100), −> BookDueDate datetime −> ); Query OK, 0 rows affected (0.57 sec)
Now you can insert some records in the table. The query is as follows −
mysql> insert into selectDataOnYearandMonthDemo values(101,'C in Depth',date_add(now(),interval -1 year)); Query OK, 1 row affected (0.50 sec) mysql> insert into selectDataOnYearandMonthDemo values(102,'Let us C',date_add(curdate(),interval 1 year)); Query OK, 1 row affected (0.12 sec) mysql> insert into selectDataOnYearandMonthDemo values(103,'SCJP Java','2018-12-5'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectDataOnYearandMonthDemo values(104,'Introduction to C++','2016-4-5'); Query OK, 1 row affected (0.24 sec)
Display all records which we inserted above. The query to display all records is as follows −
mysql> select *from selectDataOnYearandMonthDemo;
The following is the output −
+--------+--------------------+---------------------+ | BookId | BookName | BookDueDate | +--------+--------------------+---------------------+ | 101 | C in Depth | 2017-12-05 21:44:20 | | 102 | Let us C | 2019-12-05 00:00:00 | | 103 | SCJP Java | 2018-12-05 00:00:00 | | 104 | Introduction to C++| 2016-04-05 00:00:00 | +--------+--------------------+---------------------+ 4 rows in set (0.00 sec)
Here is the query to select year and month. The query is as follows −
mysql> select *from selectDataOnYearandMonthDemo where YEAR(BookDueDate) = 2019 AND MONTH(BookDueDate) = 12;
The following is the output −
+--------+----------+---------------------+ | BookId | BookName | BookDueDate | +--------+----------+---------------------+ | 102 | Let us C | 2019-12-05 00:00:00 | +--------+----------+---------------------+ 1 row in set (0.05 sec)
- Related Articles
- MySQL to fetch records based on a specific month and year?
- Select total from a MySQL table based on month
- MySQL query to get result by month and year based on condition?
- MySQL datatype to store month and year only?
- How to select month and year from dates in MySQL?
- MySQL SELECT query to return records with specific month and year
- Group month and year in MySQL?
- Select and filter the records on month basis in a MySQL table?
- Selecting data from a MySQL table based on a specific month?
- Extract the month and year in the following format: “mm-yyyy” (month year) along with all columns in MySQL?
- Format MySQL date and convert to year-month-day
- MySQL query to fetch date with year and month?
- Filter the records of current day, month and year in MySQL?
- How can we extract the Year and Month from a date in MySQL?
- How can fetch records from specific month and year in a MySQL table?

Advertisements