Document

sql如何分组选择显示最新的一条数据

怎样在数据库的一个表里筛选出每一人的时间最新的一条记录?用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

posted @ 2017-12-04 12:58  从未被超越  阅读(980)  评论(0编辑  收藏  举报