从分组的MySQL数据获取最新的日期 [英] Get the latest date from grouped MySQL data

查看:270
本文介绍了从分组的MySQL数据获取最新的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的数据库中有以下数据:

I have the following data in my database:

|NO | model | date     | 
+---+-------+----------+
|1  | bee   |2011-12-01|
|2  | bee   |2011-12-05|
|3  | bee   |2011-12-12|
|4  | tar   |2011-12-13|

我想获取每个模型组的最新日期:

I want to get the latest date of each model group:

| model | date     | 
+-------+----------+
| bee   |2011-12-12|
| tar   |2011-12-13|

我试过:

SELECT model, date 
FROM doc
WHERE date ........????? //what is the next?
GROUP BY model


推荐答案

每个模型的最大日期?

SELECT model, max(date) FROM doc
GROUP BY model

如果您正在寻找与整个表的最大日期匹配的所有模型...

If you're looking for all models matching the max date of the entire table...

SELECT model, date FROM doc
WHERE date IN (SELECT max(date) FROM doc)

这篇关于从分组的MySQL数据获取最新的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆