Get the latest date from grouped MySQL data

前端 未结 7 897
一生所求
一生所求 2020-11-27 04:29

I have the following data in my database:

|NO | model | date     | 
+---+-------+----------+
|1  | bee   |2011-12-01|
|2  | bee   |2011-12-05|
|3  | bee   |2         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 05:13

    Subquery giving dates. We are not linking with the model. So below query solves the problem.

    If there are duplicate dates/model can be avoided by the following query.

    select t.model, t.date
    from doc t
    inner join (select model, max(date) as MaxDate from doc  group by model)
    tm on t.model = tm.model and t.date = tm.MaxDate
    

提交回复
热议问题