mysql选择具有多个结果的最新日期

时间:2014-03-07 04:08:11

标签: mysql database

我有一个名为DETAILS的表,其中包含详细信息:

CODE | NAME    | ENTRY-DATE | NEXT-ENTRY-DATE
A001 | Adam    | 2010-01-01 | 2010-05-01
B001 | Bob     | 2010-11-01 | 2012-02-11
C001 | Charlie | 2010-01-01 | 2010-02-01
D001 | Dexton  | 2010-01-01 | 2013-12-11
A001 | Adam    | 2010-05-01 | 2013-05-15
B001 | Bob     | 2012-02-11 | 2013-02-11

我想要的结果是:

CODE | NAME    | ENTRY-DATE | NEXT-ENTRY-DATE
C001 | Charlie | 2010-01-01 | 2010-02-01
D001 | Dexton  | 2010-01-01 | 2013-12-11
A001 | Adam    | 2010-05-01 | 2013-05-15
B001 | Bob     | 2012-02-11 | 2013-02-11

我想要的是每个代码的最新条目。我该怎么做?

2 个答案:

答案 0 :(得分:2)

SELECT * FROM details GROUP BY code;

答案 1 :(得分:1)

使用max()将确保您获得的值是正确的:

SELECT CODE, NAME, MAX(ENTRY-DATE), NEXT-ENTRY-DATE
FROM DETAILS
GROUP BY CODE;