MySQL: Select N rows, but with only unique values in one column

前端 未结 5 799
醉梦人生
醉梦人生 2020-12-23 10:07

Given this data set:

ID  Name            City            Birthyear
1   Egon Spengler   New York        1957
2   Mac Taylor      New York        1955
3   Sara         


        
5条回答
  •  执念已碎
    2020-12-23 10:19

    This is probably not the most elegant and quickest solution, but it should work. I am looking forward the see the solutions of real database gurus.

    select p.* from people p,
    (select city, max(age) as mage from people group by city) t
    where p.city = t.city and p.age = t.mage
    order by p.age desc
    

提交回复
热议问题