按最大日期分组

时间:2013-12-10 07:20:17

标签: mysql group-by

我想从表中选择数据,按此数据分组,最大值为日期值。

在我的表中,我有4列 - id, message_id, client_id and date。列id是唯一的并且自动递增,而message_id和client_id具有重复值。约会几乎是独一无二的 我想要select all records, group by message_id and client_id, that has date maximum

我的查询是 -

SELECT *,MAX(`date`) AS `maxdate` FROM `table_name` group by `message_id`,`client_id` order by `date` desc

但是这并没有给出具有最大值的日期。 我收到第一次约会的分组记录。

请帮忙,告诉我正确的查询,我对mysql很新。

1 个答案:

答案 0 :(得分:0)

尝试这个查询 - 这就是我在Oracle中的表现:

select n1.id, n1.message_id, n1.client_id, n1.date
from shailjas_note n1
where n1.date = (select max(n2.date)
                  from shailjas_note n2
                  where n1.message_id = n2.message_id
                      and n1.client_id = n2.client_id)
相关问题