mysql - 枚举类型和 MySQL

标签 mysql enums

我有一个简单的任务,但我被困住了。 我有表login_history

`login_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`login_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`login_action` enum('login','logout') NOT NULL,
`user_id` int(11) unsigned NOT NULL, (this one is foreign key)

任务:编写一个查询,查找 2012 年 9 月星期三注销次数最多的用户。

正如你所看到的,我有一个 enum 类型的 login_action,我需要找到哪个用户在某个特定的日子里注销最多。这就是我到目前为止所做的,我只需要朝着正确的方向插入一点点,有人告诉我我这里错了..

SELECT fullname FROM user WHERE user_id = (
SELECT user_id FROM login_history WHERE (user_id,login_action) = (
    SELECT user_id, COUNT(login_action) FROM login_history WHERE login_action = 'logout' AND login_time = (
        SELECT login_time FROM login_history WHERE YEAR(login_time) = 2012 AND MONTH(login_time) = 9 AND DAYOFWEEK(login_time) = 3)));

最佳答案

试试这个:

select u.fullname from (select count(*) n,user_id 
from login_history where 
login_time between '2012-09-01' and '2012-10-01' and dayofweek(login_time) = 3 and login_action = 'logout'
group by user_id order by n desc limit 1) a, user u where a.user_id = u.user_id

为了获得良好的性能,请确保您在 login_time 列上有一个键。

关于mysql - 枚举类型和 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589054/

相关文章:

mysql - Solr - 数据导入处理程序 - 完全导入 - 默认情况下 Clean=False?

mysql - INSERT INTO 与 SELECT 和 rand() 混淆变量

PHPMailer 无法向跟踪设备发送消息

enums - 等同于 C 枚举的 Common Lisp

php - 在mysql中从第二列开始选择表列名

mysql - Pentaho错误: Duplicate entry for non primary key

java 编译与运行时计算

visual-studio - 有没有办法在程序未运行时获取 Visual Studio 中枚举的整数值?

haskell - 计数枚举值的频率

java - 如何在 intellij 中搜索枚举文件类型