MySQL Select to get users who have logged in today?

MySQLMySQLi Database

To get the users logged in today, use the below syntax. Here, we are expecting that your datetime field is a string type −

select yourColumnName1,yourColumnName2,yourColumnName3,...N
from youTableName
WHERE STR_TO_DATE(yourColumnName1, ‘format’') =CURDATE();

Let’s say we have the following “DateEqualToday “ table that stores users first and last name with the login date −

+------+------------+-----------+------------+
| Id   | First_Name | Last_Name | LoginDate  |
+------+------------+-----------+------------+
|    1 | James      | Smith     | 20-12-2018 |
|    2 | Carol      | Taylor    | 21-12-2017 |
|    3 | John       | Smith     | 21-12-2018 |
|    4 | Maria      | Garcia    | 22-12-2018 |
|    5 | Mike       | Davis     | 21-12-2018 |
|    6 | Bob        | Wilson    | 21-12-2018 |
+------+------------+-----------+------------+
6 rows in set (0.00 sec)

Here is the query to filter which users have logged in today. In this query, compare your date with curdate() function because curdate() gives only the current date −

mysql> select Id,First_Name,LoginDate
   -> from DateEqualToday WHERE STR_TO_DATE(LoginDate, '%d-%m-%Y')
=CURDATE();

Output

+------+------------+------------+
| Id   | First_Name | LoginDate  |
+------+------------+------------+
|    3 | John       | 21-12-2018 |
|    5 | Mike       | 21-12-2018 |
|    6 | Bob        | 21-12-2018 |
+------+------------+------------+
3 rows in set (0.00 sec)
raja
Published on 10-Jan-2019 11:06:40
Advertisements