
- 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
Filter the records of current day, month and year in MySQL?
Let’s say you have a table with UserLoginTime column wherein we have stored some values for sample. This is the login time of users and we want to filter all these records on the basis of current day, month and year i.e. the current date. We will be
Let us now create the table we discussed above
mysql> create table userLoginInformation - > ( - > UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > UserName varchar(20), - > UserLoginTime datetime - > ); Query OK, 0 rows affected (0.79 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into userLoginInformation(UserName,UserLoginTime) values('John','2016-02-12'); Query OK, 1 row affected (0.18 sec) mysql> insert into userLoginInformation(UserName,UserLoginTime) values('Carol','2019-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into userLoginInformation(UserName,UserLoginTime) values('Bob','2019-02-19'); Query OK, 1 row affected (0.12 sec) mysql> insert into userLoginInformation(UserName,UserLoginTime) values('Sam','2018-02-19'); Query OK, 1 row affected (0.16 sec) mysql> insert into userLoginInformation(UserName,UserLoginTime) values('Larry','2017-04-18'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from userLoginInformation;
The following is the output
+--------+----------+---------------------+ | UserId | UserName | UserLoginTime | +--------+----------+---------------------+ | 1 | John | 2016-02-12 00:00:00 | | 2 | Carol | 2019-01-31 00:00:00 | | 3 | Bob | 2019-02-19 00:00:00 | | 4 | Sam | 2018-02-19 00:00:00 | | 5 | Larry | 2017-04-18 00:00:00 | +--------+----------+---------------------+ 5 rows in set (0.00 sec)
Now, we will filter records on the basis of current date using the YEAR() and now(). Here, we are using now() to get the current date
mysql> select *from userLoginInformation where YEAR(UserLoginTime)=YEAR(now());
The following is the output
+--------+----------+---------------------+ | UserId | UserName | UserLoginTime | +--------+----------+---------------------+ | 2 | Carol | 2019-01-31 00:00:00 | | 3 | Bob | 2019-02-19 00:00:00 | +--------+----------+---------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Fetch date records comparing with the current date’s day and month in MySQL
- How to compare Year, Month and Day in a MySQL query and display matching records
- How to get current day, month and year in Java 8?
- Select last day of current year in MySQL?
- Format MySQL date and convert to year-month-day
- Select and filter the records on month basis in a MySQL table?
- How to display first day and last day of the month from date records in MySQL?
- Create date from day, month, year fields in MySQL?
- MySQL query to order by current day and month?
- How to sum current month records in MySQL?
- How to get last day of the current month in MySQL?
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Convert day of year to day of month in Java
- MySQL SELECT query to return records with specific month and year
- MySQL to fetch records based on a specific month and year?

Advertisements