MySQL搜索特定列单元字符串的最新条目

时间:2014-09-01 00:39:36

标签: php mysql unique

我对MySQL和PHP非常陌生,我想让我的页面告诉我每个名字的最新条目,但不仅仅是表格的最新条目。

表格看起来像这样,

55 | Curtis | present | 2014-06-22
87 | Curtis | present | 2014-06-22
56 | James  | absent  | 2014-08-25
57 | Curtis | late    | 2014-08-25
48 | Will   | present | 2014-08-25
47 | James  | present | 2014-08-18
43 | Will   | present | 2014-08-18

我希望它像这样输出,

43 | Will   | present | 2014-08-25
47 | James  | present | 2014-08-25
57 | Curtis | late    | 2014-08-25

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

假设您的id是增量的(即,更新的记录具有更大的id值),可以通过使用获取id的最大值的嵌套查询来解决此问题。每个name

select a.*
from yourTable as a
     inner join (
         select max(id) as max_id 
         from yourTable 
         -- Add any WHERE conditions here (for example: WHERE status='present')
         group by name
     ) as b on a.id = b.max_id
-- WHERE conditions go here
-- ORDER BY columns go here