0

我需要知道如何获取访问代码 (IdAccess) 某个用户(使用 IdUser 标识)使用不同查询从表AccessTbl进行的最旧(或更近)访问日期。每个用户。

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)

后来,我尝试了:

按 IdUser 从 AccessTbl 组中选择 IdAcess、IdUser、MAX(TheDate);

拥有...

+---------+--------+--------------+
| 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)

感谢您的宝贵帮助。

4

3 回答 3

0

如果您想同时获取最旧和最新的,则需要使用子查询将现有日期(由 IdUser 过滤)与选定的最小和最大日期进行比较。

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

于 2013-04-18T04:47:04.763 回答
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

要一次获取所有用户的最新行,只需跳过 IdUser 条件;

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

一个用于测试的 SQLfiddle

于 2013-04-18T04:47:41.090 回答
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 Fiddle: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 Fiddle:http ://sqlfiddle.com/#!2/4b0e68/44

于 2013-04-18T04:56:39.037 回答