2013-02-04 94 views
0

我想問一個簡單的查詢我可以用它來了解每個最新的登錄日期爲我的用戶...... 感謝查詢的最近的登錄日期爲當前日期爲每個用戶

+----+---------+------------+-----------+---------------------+ 
| id | version | first_name | last_name | login_detail  | 
+----+---------+------------+-----------+---------------------+ 
| 1 |  0 | Mark  | Yap2  | 2013-02-01 16:00:23 | 
| 2 |  0 | Mark  | Yap2  | 2013-02-01 16:00:38 | 
| 3 |  0 | Mark  | Yap2  | 2013-02-04 13:38:41 | 
| 4 |  0 | john  | doe  | 2013-02-04 13:50:39 | 
| 5 |  0 | Mark  | Yap2  | 2013-02-04 13:59:01 | 
| 6 |  0 | Mark  | Yap2  | 2013-02-04 14:04:25 | 
| 7 |  0 | john  | doe  | 2013-02-04 14:09:47 | 
| 8 |  0 | Mark  | Yap2  | 2013-02-04 14:14:40 | 
| 9 |  0 | Mark  | Yap2  | 2013-02-04 14:15:36 | 
| 10 |  0 | john  | doe  | 2013-02-04 14:16:11 | 
| 11 |  0 | Mark  | Yap2  | 2013-02-04 14:23:38 | 
| 12 |  0 | john  | doe  | 2013-02-04 14:26:53 | 
| 13 |  0 | john  | doe  | 2013-02-04 15:50:45 | 
| 14 |  0 | john  | doe  | 2013-02-04 15:55:53 | 
| 15 |  0 | john  | doe  | 2013-02-04 15:59:15 | 
| 16 |  0 | john  | doe  | 2013-02-04 16:05:15 | 
| 17 |  0 | john  | doe  | 2013-02-04 16:09:54 | 
+----+---------+------------+-----------+---------------------+ 

此示例表... 我想獲得最新的登錄記錄每個用戶... 這與ID爲17和11的2個不同的用戶

+0

'當前' 日期可以過去?或者你只是在尋找每個用戶的最近登錄日期? – Strawberry

回答

1

假設你有一個名爲tableName表格中的,並具有列:userID,last_login。您可以使用MAX這是彙總函數知道用戶的最新登錄日期。

SELECT userID, 
     MAX(last_login) most_current_login 
FROM tableName 
GROUP BY userID 
+0

它不起作用..它只打印1個用戶....我的領域是姓氏名字和登錄(時間/日期) –

+0

你能給swamp記錄與期望的結果? –

0

查詢:

SQLFIDDLEexample

SELECT first_name, 
     last_name, 
     MAX(login_detail) latest_login_record 
FROM Table1 
GROUP BY first_name, 
      last_name 

結果:

| FIRST_NAME | LAST_NAME |    LATEST_LOGIN_RECORD | 
------------------------------------------------------------ 
|  john |  doe | February, 04 2013 16:09:54+0000 | 
|  Mark |  Yap2 | February, 04 2013 14:23:38+0000 | 
相關問題