2015-10-29 88 views
2

給定一個表具有以下字段之間的事件「會話」:MySQL的:創建兩個日期

username | event    | time (hh:mm:ss)  
user2 | login    | 03:27:17 
user2 | check messages  | 03:31:31 
user2 | view profile  | 03:32:01 
user2 | logout    | 03:32:48 
user3 | login    | 13:00:59 
user3 | change billing info | 13:03:11 
user3 | logout    | 13:03:32 

我試圖創建用戶「會話」,使用首次作爲「登錄」時間和最後一次作爲「註銷」時間。結果表應該是:

username | event    | login time | event time | logout time 
user2 | login    | 03:27:17 | 03:27:17 | 03:32:48 
user2 | check messages  | 03:27:17 | 03:31:31 | 03:32:48 
user2 | view profile  | 03:27:17 | 03:32:01 | 03:32:48 
user2 | logout    | 03:27:17 | 03:32:48 | 03:32:48 
user3 | login    | 13:00:59 | 13:00:39 | 13:03:32 
user3 | change billing info | 13:00:59 | 13:03:11 | 13:03:32 
user3 | logout    | 13:00:59 | 13:03:32 | 13:03:32 

我曾嘗試使用分鐘(時間)和最大值(時間)來獲得登錄和退出時間,但是當我這樣做,我得到的登錄,事件和註銷倍混合。

SELECT login.eventTime, actualEvent.eventTime, logout.eventTime 
FROM tableName login, tableName actualEvent, tableName logout 
WHERE login.username = actualEvent.username 
AND actualEvent.username = logout.username 
AND login.eventTime = 
    (SELECT MIN(minTime.eventTime) 
    FROM tableName minTime 
    WHERE minTime.username = login.username) 
AND logout.eventTime = 
    (SELECT MAX(maxTime.eventTime) 
    FROM tableName maxTime 
    WHERE maxTime.username = login.username) 
AND login.eventTime < logout.eventTime; 

任何幫助將不勝感激。

編輯: @Bernd Buffen的回答很好,除非時間發生在多個日期。例如:

username | event    | time (hh:mm:ss)  
user2 | login    | 08/11/2015 03:27:17 
user2 | check messages  | 08/11/2015 03:31:31 
user2 | view profile  | 08/11/2015 03:32:01 
user2 | logout    | 08/11/2015 03:32:48 
user3 | login    | 08/11/2015 13:00:59 
user3 | change billing info | 08/11/2015 13:03:11 
user3 | logout    | 08/11/2015 13:03:32 
user2 | login    | 08/12/2015 04:00:00 
user2 | change billing info | 08/12/2015 04:03:22 
user2 | logout    | 08/12/2015 04:08:17 

在此事件中,我的輸出表的會話結束時間提前了幾天。有關如何解決此問題的任何建議?

+0

粘貼您使用當前的SQL命令,所以我們可以來看看。 – jpaljasma

+0

爲什麼查看消息和查看配置文件是在實際登錄時間? – Tarek

回答

1

假設你的表名是測井公司,我認爲這將有助於(未選中):

SELECT l.username, l.event, login_time, event_time, logout_time 
FROM loggings l 

LEFT JOIN (
    SELECT MIN(`time`) AS login_time, username FROM loggings 
    GROUP BY username 
) login_tbl 
ON l.username=login_tbl.username 

LEFT JOIN (SELECT `time` AS event_time, username FROM loggings) event_tbl 
ON l.username=event_tbl.username 

LEFT JOIN (
    SELECT MAX(`time`) AS logout_time, username FROM loggings 
    GROUP BY username 
) AS logout_tbl 
ON l.username=logout_tbl.username 
0

您還需要確保如果同一用戶有兩個區段,每個都有其正確的登錄和註銷事件,您可以將正確的登錄/註銷事件與您選擇的事件進行匹配。請注意,要實現此目標,您必須事實上最大化登錄事件的時間,並儘量減少註銷事件的時間。當然,兩者都應該停留在實際事件的正確方面。所以我會提出以下建議:

SELECT  actualEvent.username 
     , actualEvent.event 
     , MAX(login.eventTime) loginTime 
     , actualEvent.eventTime 
     , MIN(logout.eventTime) logoutTime 
FROM  tableName actualEvent 
INNER JOIN tableName login 
     ON actualEvent.username = login.username 
     AND actualEvent.evenTime >= login.eventType 
     AND login.eventType = 'login' 
INNER JOIN tableName logout 
     ON actualEvent.username = logout.username 
     AND actualEvent.evenTime <= logout.eventType 
     AND logout.eventType = 'logout' 
GROUP BY actualEvent.username 
     , actualEvent.event 
     , actualEvent.eventTime 
ORDER BY actualEvent.eventTime; 
0

試試這個。我希望這就是你想要什麼

SELECT 
    t.`username`, 
    t.`event`, 
    min(l.`time`) AS 'event_login', 
    t.`time` AS 'event_time', 
    max(l.`time`) AS 'event_logout' 
FROM mytable t 
LEFT JOIN mytable l ON t.username = l.username 
GROUP BY t.username,t.event 
ORDER BY `username` ASC, t.time; 

結果:

+----------+---------------------+-------------+------------+--------------+ 
| username | event    | event_login | event_time | event_logout | 
+----------+---------------------+-------------+------------+--------------+ 
| user2 | login    | 03:27:17 | 03:27:17 | 03:32:48  | 
| user2 | check messages  | 03:27:17 | 03:31:31 | 03:32:48  | 
| user2 | view profile  | 03:27:17 | 03:32:01 | 03:32:48  | 
| user2 | logout    | 03:27:17 | 03:32:48 | 03:32:48  | 
| user3 | login    | 13:00:59 | 13:00:59 | 13:03:32  | 
| user3 | change billing info | 13:00:59 | 13:03:11 | 13:03:32  | 
| user3 | logout    | 13:00:59 | 13:03:32 | 13:03:32  | 
+----------+---------------------+-------------+------------+--------------+ 
7 rows in set (0.00 sec) 
+0

這適用於日期不同的情況(用戶2在兩個不同的日期有會話)。有什麼建議麼? – bspitch1

+0

@ bspitch1我可否請你給我一個從user2輸入日期的樣本,它不起作用,然後將更正它 –

+0

如果登錄時間是日期時間值,並且用戶登錄,檢查消息並註銷兩個不同的日期。所以從上面的表中,添加:user2 login @ 12/21/2014 14:17:07,user2檢查消息@ 12/21/2014 14:18:00,user2註銷@ 12/21/2014 14:18:59 。這有意義嗎? – bspitch1