In general I prefer to tackle these cases with a JOIN to a sub query, as in the following example:
SELECT s.id, s.school, s.address, s.city, s.phone, s.email, s.fax
FROM schools s
JOIN (SELECT MAX(id) as max_id
FROM schools
GROUP BY city) sub_s ON (sub_s.max_id = s.id);
The JOIN
is practically restricting your result set to entries with distinct cities. This is also assuming that you want to select the entry with the highest id when you have multiple entries for each city. If you prefer to choose the lowest id, you can use the MIN()
aggregate function in the sub query instead of MAX()
.