Select the latest MySQL Data but unique resort

后端 未结 5 1446
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 16:41

I have a 1 table database that has a list of advertisements. I am trying to grab the LATEST advertisement for EACH resort. I know it should pr

5条回答
  •  太阳男子
    2021-01-21 16:57

    select t.*
    from YourTable t
    join
        (select resort, max(dateAdded) dateAdded
        from YourTable
        group by resort) m on t.dateAdded = m.dateAdded and t.resort = m.resort
    order by t.resort
    

    First group the rows by resort to find the max of dateAdded, then query (join) the rows that have the same dateAdded with the max.
    One problem is, if the same resort b is added in the same time, twice or more, it will take only the first row. But I think is slightly possible.

提交回复
热议问题