简体   繁体   English

枚举类型和MySQL

[英]Enum type and MySQL

i have simple task and i got stucked on it. 我的任务很简单,我陷入了困境。 I have table login_history 我有表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)

TASK: Write a query which will find a user who had most logouts on Wednesdays in September 2012. 任务:编写一个查询,该查询将找到一个在2012年9月星期三注销次数最多的用户。

As you can see i have login_action which is enum type and i need to find which user had most logouts on some specific day.. This is what i done so far i just need little push in right direction, someone to tell me where i am wrong here.. 如您所见,我有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)));

Try this: 尝试这个:

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

For good performance, make sure you have a key on login_time column. 为了获得良好的性能,请确保在login_time列上具有键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM