Mysql 将字段分组以显示最近日期
我有一个我似乎想不出来的问题。我试图列出最终用户项目(只是一个示例),只显示最近的注释Mysql 将字段分组以显示最近日期,mysql,sql,database,Mysql,Sql,Database,我有一个我似乎想不出来的问题。我试图列出最终用户项目(只是一个示例),只显示最近的注释 ID Name EventDate Type 1 PC 12/12/2012 End User Items 1 PC 11/12/2012 End User Items 1 PC 10/11/2012 End User Items 2 Mobile 12/12/2012 End User Items 2 Mob
ID Name EventDate Type
1 PC 12/12/2012 End User Items
1 PC 11/12/2012 End User Items
1 PC 10/11/2012 End User Items
2 Mobile 12/12/2012 End User Items
2 Mobile 11/01/2012 End User Items
2 Mobile 12/12/2011 End User Items
3 Server 12/12/2013 Server
所以我会尝试这样的方法:
select * from systems where type = "End User Items" group by ID
但结果仍然无法显示最新日期。
任何帮助都将不胜感激 您是否尝试按如下方式获取
MAX
日期
SELECT Name,
MAX(EventDate),
Type
FROM systems
WHERE type = 'End User Items'
GROUP BY Name, Type
您可以使用子查询获取ID和最大日期,然后使用ID和最大日期在系统表上重新联接
SELECT s2.Name, s.EventDate
FROM system s2
INNER JOIN (SELECT ID, MAX(EventDate) EventDate
FROM systems
WHERE type = 'End User Items'
GROUP BY ID) AS s on s2.ID = s.ID AND s2.EventDate = s.EventDate
将要在不进行摘要的情况下显示的列定义为分组依据字段。选择这些字段和计算的摘要函数列(例如max()、sum()、count())
您可能想查看ID和名称。类型在这里是多余的,因为您只需要一种类型。您希望获取组中的max()日期。返回的所有字段必须是摘要函数或按列分组。该示例输入的所需输出是什么?是否要查看最近的注释?注释指的是哪个字段?
SELECT ID, Name, MAX(EventDate) as LastDate
FROM systems
WHERE type = 'End User Items'
GROUP BY ID, Name