
- 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
How to compare Year, Month and Day in a MySQL query and display matching records
For this, you can use DATE(). Let us first create a table −
mysql> create table DemoTable864(DueDateTime timestamp); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable864 values('2019-01-10 12 −34 −55'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable864 values('2016-12-11 11 −12 −00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable864 values('2015-04-01 10 −00 −00'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable864 values('2017-05-20 04 −40 −10'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable864;
This will produce the following output −
+---------------------+ | DueDateTime | +---------------------+ |2019-01-10 12 −34 −55| |2016-12-11 11 −12 −00| |2015-04-01 10 −00 −00| |2017-05-20 04 −40 −10| +---------------------+ 4 rows in set (0.00 sec)
Following is the query to compare year, month and day −
mysql> select *from DemoTable864 where date(DueDateTime)='2015-04-01';
This will produce the following output −
+-----------------------+ | DueDateTime | +-----------------------+ | 2015-04-01 10 −00 −00 | +-----------------------+ 1 row in set (0.04 sec)
- Related Articles
- Filter the records of current day, month and year in MySQL?
- MySQL SELECT query to return records with specific month and year
- How to display first day and last day of the month from date records in MySQL?
- Display month names and year from a column with date records with MySQL
- Format MySQL date and convert to year-month-day
- MySQL query to subtract date records with week day and display the weekday with records
- MySQL to fetch records based on a specific month and year?
- MongoDB query to search date records using only Month and Day
- MySQL query to fetch date with year and month?
- How can fetch records from specific month and year in a MySQL table?
- MySQL query to order by current day and month?
- Compare only day and month with date field in MySQL?
- How to get a Date from year, month and day in Java?
- How to get current day, month and year in Java 8?
- How to convert year, month, and day of the month into a complete date in R?

Advertisements