根据字段仅选择表的最新行

时间:2016-10-17 21:23:32

标签: mysql

鉴于下表:

Column1   Column2        Column3
Name1     "2016-05-11"   Value1
Name2     "2016-05-11"   Value2
Name2     "2015-05-17"   Value3
Name3     "2014-07-31"   Value4
Name4     "2011-07-31"   Value5
Name4     "2013-07-31"   Value6
Name4     "2016-09-31"   Value7

如何为每个Column1值仅选择Column2中的最新值。 所以查询结果如下:

Column1   Column2        Column3
Name1     "2016-05-11"   Value1
Name2     "2016-05-11"   Value2
Name3     "2014-07-31"   Value4
Name4     "2016-09-31"   Value7

1 个答案:

答案 0 :(得分:1)

您可以使用where in和select max group by

select column1, column2, column3
from my_table 
where ( column1, column2 ) in 
          (select column1, max(column2)
                from my_table
                group by column1);