Select the latest MySQL Data but unique resort

后端 未结 5 1447
佛祖请我去吃肉
佛祖请我去吃肉 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:46

    Assuming the Id column is unique:

    SELECT T3.*
    FROM yourtable AS T3
    JOIN
    (
        SELECT T2.resort, T2.date_added, MAX(T2.id) AS id
        FROM yourtable AS T2
        JOIN
        (
            SELECT resort, MAX(date_added) AS date_added
            FROM yourtable
            GROUP BY resort
        ) AS T1
        ON T1.resort = T2.resort AND T1.date_added = T2.date_added
        GROUP BY T2.resort, T2.date_added
    ) AS T4
    ON T4.id = T3.id
    

提交回复
热议问题