Home  >  Article  >  Database  >  MySQL chooses to get the user logged in today?

MySQL chooses to get the user logged in today?

PHPz
PHPzforward
2023-08-26 22:05:13553browse

MySQL 选择获取今天登录的用户?

To log a user in today, use the following syntax. Here we are expecting your datetime field to be of string type -

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

Suppose we have the following "DateEqualToday" table which stores the first and last name of the user along 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 It is a query that filters users logged in today. In this query compare your date with curdate() function as curdate() only gives 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)

The above is the detailed content of MySQL chooses to get the user logged in today?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete