
- 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 Select Date Equal to Today and return results for the same date?
To get today’s date, use in-built function CURDATE(). The CURDATE() gives only current date not time. With that, to get the records for the same day, you can try the following syntax −
select yourColumnName1,yourColumnName2,......,yourColumnNameN,DATE_FORMAT(yourDateColumnName, '%Y-%m-%d') from yourTableName WHERE DATE(yourDateColumnName) = CURDATE();
To understand the above concept, let us create a table. The query to create a table is as follows. One of these columns will have datetime datatype to display dates −
mysql> create table GmailSignIn −> ( −> UserId int, −> UserName varchar(200), −> DateOfSignIn datetime −> ); Query OK, 0 rows affected (1.56 sec)
Now you can insert some records into the table with the help of insert command. We have set the current date as well, which is 2018-12-06.
The query is as follows −
mysql> insert into GmailSignIn values(222111,'John',now()); Query OK, 1 row affected (0.15 sec) mysql> insert into GmailSignIn values(333222,'Johnson',curdate()); Query OK, 1 row affected (0.18 sec) mysql> insert into GmailSignIn values(444333,'Carol',date_add(curdate(),interval 1 day)); Query OK, 1 row affected (0.13 sec) mysql> insert into GmailSignIn values(555444,'David',date_add(curdate(),interval -1 day)); Query OK, 1 row affected (0.83 sec)
Display all records from the table with the help of select statement. The query is as follows −
mysql> select *from GmailSignIn;
The following is the output −
+--------+----------+---------------------+ | UserId | UserName | DateOfSignIn | +--------+----------+---------------------+ | 222111 | John | 2018-12-06 19:13:30 | | 333222 | Johnson | 2018-12-06 00:00:00 | | 444333 | Carol | 2018-12-07 00:00:00 | | 555444 | David | 2018-12-05 00:00:00 | +--------+----------+---------------------+ 4 rows in set (0.00 sec)
Here is the query to select date equal to today and display the records for the same date −
mysql> select UserId,UserName,DateOfSignIn,DATE_FORMAT(DateOfSignIn, '%Y-%m-%d') from GmailSignIn −> where date(DateOfSignIn) = curdate();
The following is the output −
+--------+----------+---------------------+---------------------------------------+ | UserId | UserName | DateOfSignIn | DATE_FORMAT(DateOfSignIn, '%Y-%m-%d') | +--------+----------+---------------------+---------------------------------------+ | 222111 | John | 2018-12-06 19:13:30 | 2018-12-06 | | 333222 | Johnson | 2018-12-06 00:00:00 | 2018-12-06 | +--------+----------+---------------------+---------------------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to select closest date from today?
- SELECT MySQL rows where today's date is between two DATE columns?
- MySQL DATE function to return the difference between current date and joining date
- How to get the date between TODAY and TODAY-7”?
- MySQL query to select date >= current date - 3 weeks?
- Select date from MySQL and format to text?
- MySQL query to select date from 00:00 to today’s date
- How to select a date less than the current date with MySQL?
- Display record with today and tomorrow’s date from a column with date record in MySQL
- Fetch date record that equals today in MySQL
- How to return today's records using DATE from DATETIME Field?
- MySQL query to select date from timestamp?
- Select dates between current date and 3 months from the current date in MySQL?
- Format date in MySQL to return MonthName and Year?
- MySQL select * and find record with current date
