MySQL SELECT WHERE DATE IS TODAY

In this article, we will be looking into MySQL select where the date is today’s date. We will be going through examples to illustrate its working.

Let us get started by making the sample data. Create a table named user_login_details, followed by inserting some rows into it.

# create the table user_login_details;  
CREATE TABLE user_login_details (
user_id INT,
user_name VARCHAR(255),
login_date DATETIME
);
# insert rows of data to user_login_details;  
INSERT INTO user_login_details(user_id,user_name,login_date) 
VALUES(1,"Justin",'2020-12-12 19:13:30'),
(2,"Veronica",(current_date()-1)),
(3,"George",'2019-11-02 09:12:30'),
(4,"Peter",'2020-03-08 07:07:20'),
(5,"Smitha",'2019-12-06 09:12:30'),
(6,"Walter",(current_date()-1)),
(7,"Daniel",'2020-09-09 09:12:30'),
(8,"Atharv",current_date());

Let us have a view of the table user_login_details by executing:

SELECT * FROM user_login_details;

Output:-

image1: user_login_details

Example: Get the details of users who have logged in today.

Observe the below query for the solution.

SELECT * FROM user_login_details WHERE DATE(login_date) = CURDATE();

Action Output Message:-

2 row(s) returned

Output:-

image_2

If we want to specify the format as well in the query, we can modify the above query as:

SELECT user_id, DATE_FORMAT(login_date, '%Y-%m-%d') FROM user_login_details WHERE DATE_FORMAT(login_date, '%Y-%m-%d') = CURDATE();

Action Output Message:-

2 row(s) returned

Output:-

image_3

Here in the above queries we are using MySQL functions DATE(expression), DATE_FORMAT(date,format), CURDATE();

  • DATE(expression): Will get the date value from the DATE or DATETIME expression passed in as a parameter.
  • DATE_FORMAT(date, format_string)Will format the date value according to the format string passed in as a parameter.
  • CURDATE(): Will return current date either in ‘YYYY-MM-DD‘ or ‘YYYYMMDD‘ format depending on if the curdate() function is used in a string or numeric context.

To get the current date as an output execute:

 SELECT CURDATE();

Output:-

image_4

The output in image_4 shows today’s system date.

READ MORE

We hope this article helped you with MySQL Select statements where the date is today's date. Good Luck!!!.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top