how to get latest record or record with max corresponding date of all distinct values in a column in mysql?

前端 未结 5 1899
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 00:11

For Example, I have table like this:

Date      | Id | Total
-----------------------
2014-01-08  1    15
2014-01-09  3    24
2014-02-04  3    24
2014-03-15  1             


        
5条回答
  •  囚心锁ツ
    2021-01-21 00:47

    You can also use Max() in sql:

    SELECT date, id, total
       FROM table as a WHERE date = (SELECT MAX(date)
       FROM table as b
      WHERE a.id = b.id
     )
    

提交回复
热议问题