在数据库中查找最新的一条记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tongjinrui/article/details/80078981
-- 方法1
select a.* 
 from table1 a
 where not exists(select 1 
                  from table1 b
                  where b.name=a.name and b.gdtime>a.gdtime)
 
-- 方法2
select a.*
 from table1 a
 inner join
 (select name,
         max(gdtime) 'maxgdtime'
  from table1 
  group by name) b on a.name=b.name and a.gdtime=b.maxgdtime

推荐方法2,用方法一运行效率不高。

猜你喜欢

转载自blog.csdn.net/tongjinrui/article/details/80078981