要让用户今天登录,请使用以下语法。在这里,我们期望您的日期时间字段是字符串类型 -
select yourColumnName1,yourColumnName2,yourColumnName3,...N from youTableName WHERE STR_TO_DATE(yourColumnName1, ‘format’') =CURDATE();
假设我们有以下“DateEqualToday”表,其中存储用户的名字和姓氏以及登录日期 -
+------+------------+-----------+------------+ | 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)
这里是过滤今天登录的用户的查询。在此查询中,将您的日期与 curdate() 函数进行比较,因为 curdate() 只给出当前日期 -
mysql> select Id,First_Name,LoginDate -> from DateEqualToday WHERE STR_TO_DATE(LoginDate, '%d-%m-%Y') =CURDATE();
+------+------------+------------+ | 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)