2013-04-18 89 views
0

我需要知道如何獲得訪問代碼(IdAccess)最近(或最近)的訪問日期,某些用戶(用IdUser標識)從表AccessTbl使用不同的查詢。每個用戶。錯誤的查詢MySQL選擇最舊或最新的記錄日期

CREATE TABLE AccessTbl ( 
IdAcess VARCHAR(16) UNIQUE NOT NULL, 
IdUser VARCHAR(16) NOT NULL, 
TheDate DATE NOT NULL 
); 
INSERT INTO AccessTbl (IdAcess, IdUser, TheDate) VALUES 
('A00', '0', '1983-12-30'), 
('A01', '0', '2004-09-09'), 
('A02', '1', '2013-02-01'), 
('A03', '1', '2012-05-09'), 
('A04', '2', '1983-12-30'), 
('A13', '2', '2013-03-01'), 
('A05', '2', '2004-09-09'), 
('A06', '3', '2013-02-01'), 
('A07', '3', '2012-05-09'), 
('A08', '4', '1983-12-30'), 
('A09', '4', '2004-09-09'), 
('A10', '5', '2013-04-01'), 
('A12', '5', '2013-03-01'), 
('A11', '5', '2012-05-01'); 

實施例:

用戶5最近的是「A10」與最古老的「A11」 用戶4最近的是「A09」與最古老的「A08」

但是,我只需要在一個查詢中查看所有用戶的所有最新日期。

我試着用...

SELECT IdAcess, IdUser, TheDate 
FROM AccessTbl 
WHERE (
    TheDate IN (SELECT MAX(TheDate) FROM AccessTbl GROUP BY IdUser) 
) GROUP BY IdUser; 

但我有:

+---------+--------+ 
| IdAcess | IdUser | 
+---------+--------+ 
| A01  | 0  | 
| A02  | 1  | 
| A05  | 2  | //BAD!!!!! must be A13 
| A06  | 3  | 
| A09  | 4  | 
| A10  | 5  | 
+---------+--------+ 
6 rows in set (0.13 sec) 

後來,我試着用:

SELECT IdAcess,ID用戶,MAX(TheDate) FROM AccessTbl GROUP BY IdUser;

有...

+---------+--------+--------------+ 
| IdAcess | IdUser | MAX(TheDate) | 
+---------+--------+--------------+ 
| A00  | 0  | 2004-09-09 | //Must be A01 
| A02  | 1  | 2013-02-01 | 
| A04  | 2  | 2013-03-01 | //Must be A13 
| A06  | 3  | 2013-02-01 | 
| A08  | 4  | 2004-09-09 | //Must be A09 
| A10  | 5  | 2013-04-01 | 
+---------+--------+--------------+ 
6 rows in set (0.00 sec) 

謝謝您的寶貴幫助。

+0

你想要什麼?只有最老的IdAcess?你想要兩個 - 最老的還是最近的? – 2013-04-18 04:46:18

+0

對不起,我很困惑....我需要所有用戶的所有最新日期... – Anita 2013-04-18 13:56:57

回答

0

如果你想獲得這兩個最古老,同時最新的,你需要使用子查詢來比較現有的日期(ID用戶所過濾)與選定的最小和最大的日期。

SELECT IdAcess 
FROM AccessTbl 
WHERE IdUser = ? 
AND (
    TheDate IN (SELECT MAX(TheDate) FROM AccessTbl WHERE IdUser = ?) 
    OR TheDate IN (SELECT MIN(TheDate) FROM AccessTbl WHERE IdUser = ?) 
) 

http://sqlfiddle.com/#!2/38594/20/0

+0

爲什麼'IN'而不是'='? – Barmar 2013-04-18 04:51:43

+0

@Barmar我猜'''會工作得很好..我沒有*好*理由 – 2013-04-18 04:52:42

0

你可以做任何最新或最古老的使用LEFT JOIN;

獲取用戶5的最新條目;

SELECT * 
FROM AccessTbl a 
LEFT JOIN AccessTbl b 
    ON a.TheDate < b.TheDate AND a.IdUser = b.IdUser 
WHERE b.TheDate IS NULL AND a.IdUser = '5' 

爲了得到最古老的,只是翻轉<>;

ON a.TheDate > b.TheDate AND a.IdUser = b.IdUser 

要獲取最新一行所有用戶一次,只是跳過ID用戶狀態;

SELECT * 
FROM AccessTbl a 
LEFT JOIN AccessTbl b 
    ON a.TheDate < b.TheDate AND a.IdUser = b.IdUser 
WHERE b.TheDate IS NULL 

An SQLfiddle to test with

0

只是一種方法:

SELECT IdAcess AS Oldest, (SELECT IdAcess FROM AccessTbl 
          WHERE IdUser = 5 
          ORDER BY TheDate DESC LIMIT 1) AS Newest 
FROM AccessTbl 
WHERE IdUser = 5 
ORDER BY TheDate ASC LIMIT 1 

這裏的SQL小提琴測試:http://sqlfiddle.com/#!2/4b0e68/36


要獲得每個用戶的最新IdAcess在短短一個查詢執行此:

SELECT * 
FROM AccessTbl a 
LEFT JOIN AccessTbl b 
    ON a.TheDate < b.TheDate AND a.IdUser = b.IdUser 
WHERE b.TheDate IS NULL 
ORDER BY a.IdUser 

結果

IDACESS  IDUSER  THEDATE 
A01   0   September, 09 2004 00:00:00+0000 
A02   1   February, 01 2013 00:00:00+0000 
A13   2   March, 01 2013 00:00:00+0000 
A06   3   February, 01 2013 00:00:00+0000 
A09   4   September, 09 2004 00:00:00+0000 
A10   5   April, 01 2013 00:00:00+0000 

這裏的SQL小提琴測試:http://sqlfiddle.com/#!2/4b0e68/44