根据时间获取最新数据 SQL(每一个人或者每一项)

Posted 一佳一

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据时间获取最新数据 SQL(每一个人或者每一项)相关的知识,希望对你有一定的参考价值。

-- 方法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查询速度更快,建议使用第二种 关联查询法

 

以上是关于根据时间获取最新数据 SQL(每一个人或者每一项)的主要内容,如果未能解决你的问题,请参考以下文章