I need some help with a MySQL query.
I have this table:
Name | Date | City ------+------------+------ Peter | 2013-04-01 | Berlin Paul | 2013-03-01 | London Peter | 2013-03-01 | New York Peter | 2013-01-28 | Tokio
I need, for each name, the first date and corresponding city in the table.
I tried:
SELECT Name, MIN(Date) as MD, MIN(City) as CN FROM table GROUP BY Name
But the dates don’t corresponds to the city name, the result was:
Peter, 2013-01-028, Berlin
Can anybody give me a hint?
Thanks
You will want to use a subquery to get the
min(date)bynameand then join that result to your table on both thenameanddate:See SQL Fiddle with Demo
The result is: