簡體   English   中英

枚舉類型和MySQL

[英]Enum type and MySQL

我的任務很簡單,我陷入了困境。 我有表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列上具有鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM