I have a MySQL table (5.1.49) with three columns.
mysql> create table testme(id int auto_increment, id2 int not null, somedate datetime, primary key(id));
In my case id2 is not unique, but I want to return rows with different id2 values ββwith max somedate .
Here are some sample data.
mysql> insert into testme values (1, 5, '2012-01-02 01:01:01'),(2, 5, '2012-02-02 02:02:02'),(3, 7, '2010-01-10 11:01:33');
This question almost answers mine, but id is returned with an additional id field and id2 does not match. For id2 = 5, it returns id = 1 instead of id = 2 .
mysql> select id, id2, max(somedate) from testme group by id2; +----+-----+---------------------+ | id | id2 | max(somedate) | +----+-----+---------------------+ | 1 | 5 | 2012-02-02 02:02:02 | | 3 | 7 | 2010-01-10 11:01:33 | +----+-----+---------------------+
I expect,
+----+-----+---------------------+ | id | id2 | max(somedate) | +----+-----+---------------------+ | 2 | 5 | 2012-02-02 02:02:02 | | 3 | 7 | 2010-01-10 11:01:33 | +----+-----+---------------------+
Want an ID that matches the maximum date for each ID2
Does anyone have any ideas? Thanks