MySQL 选择以获取今天登录的用户?
要获取今天登录的用户,请使用以下语法。在此,我们希望你的 datetime 字段是字符串类型 −
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)
广告