怎样在数据库的一个表里筛选出每一人的时间最新的一条记录?用SQL语句。感谢大神啊

wenchuan408 2014-08-21 09:20:37
yhh name gdcs gdsj1 gdtime
600040407 王玲 1 0.56 2011/6/21 22:34
600040407 王玲 2 0.56 2011/6/24 10:21
600040407 王玲 3 0.56 2011/12/7 10:45
600040407 王玲 4 0.56 2012/1/15 14:01
600040407 王玲 5 0.56 2012/12/26 14:11
600040408 魏武 1 0.56 2011/6/21 22:36
600040408 魏武 2 0.56 2013/11/15 10:46
600040408 魏武 4 0.56 2014/1/19 9:12
600040408 魏武 3 0.56 2014/1/10 13:57
600040408 魏武 5 0.56 2014/1/22 10:08
600040435 于洋 1 0.56 2011/6/22 12:54
600040435 于洋 2 0.56 2013/3/11 9:16
600040435 于洋 4 0.56 2014/1/10 11:18
600040435 于洋 3 0.56 2013/12/20 15:09

我知道用 select name,MAX(gdtime) from table1 group by yhh 挑选出每个人的最新登录实际,结果只有name,gdtime
怎么将每个人的最新登录这条记录全部信息显示出来,不止name,gdtime,我还要yhh ,gdcs,gdsj1 这几列

以上拜托了!
...全文
60229 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwneil 2017-04-14
  • 打赏
  • 举报
回复
引用 6 楼 l8686555_110 的回复:
mysql取分组后最新的一条记录,下面两种方法.一种是先筛选 出最大和最新的时间,在连表查询.一种是先排序,然后在次分组查询(默认第一条),就是最新的一条数据了 #select * from t_assistant_article as a, (select max(base_id) as base_id, max(create_time) as create_time from t_assistant_article as b group by base_id ) as b where a.base_id=b.base_id and a.create_time = b.create_time #select base_id,max(create_time), max(article_id) as article_id from t_assistant_article as b group by base_id select * from (select * from t_assistant_article order by create_time desc) as a group by base_id
你这个办法最简单
yuanjun242 2017-03-31
  • 打赏
  • 举报
回复
我的问题也解决了..非常感谢4楼 最终考虑自己 还是思路有问题
  • 打赏
  • 举报
回复
哈哈,我的解决问题了,谢谢4楼
尘同学 2017-01-04
  • 打赏
  • 举报
回复
哈哈,解决问题了,谢谢4楼
唯我独想1 2015-10-29
  • 打赏
  • 举报
回复
引用 3 楼 fredrickhu 的回复:
select * from tb as t where not exists(select 1 from tb where yhh=t.yhh and  name=t. name and gdtime>t.gdtime)
查询的速度太慢了
l8686555_110 2015-09-29
  • 打赏
  • 举报
回复
mysql取分组后最新的一条记录,下面两种方法.一种是先筛选 出最大和最新的时间,在连表查询.一种是先排序,然后在次分组查询(默认第一条),就是最新的一条数据了 #select * from t_assistant_article as a, (select max(base_id) as base_id, max(create_time) as create_time from t_assistant_article as b group by base_id ) as b where a.base_id=b.base_id and a.create_time = b.create_time #select base_id,max(create_time), max(article_id) as article_id from t_assistant_article as b group by base_id select * from (select * from t_assistant_article order by create_time desc) as a group by base_id
nlh_2007 2014-12-22
  • 打赏
  • 举报
回复
能单聊吗?我也有这个问题。
唐诗三百首 2014-08-21
  • 打赏
  • 举报
回复

-- 方法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
--小F-- 2014-08-21
  • 打赏
  • 举报
回复
select * from tb as t where not exists(select 1 from tb where yhh=t.yhh and  name=t. name and gdtime>t.gdtime)
习惯性蹭分 2014-08-21
  • 打赏
  • 举报
回复


select yhh,name,gdcs,gdsj1,gdtime
from tab a where not exists(select 1 from tab where yhh=a.yhh and gdtime>a.gdtime)
xzb001 2014-08-21
  • 打赏
  • 举报
回复

/* 1 用子查询 */
select yhh, name, gdcs, gdsj1, gdtime
from table1
where exists
(
    select 1 from
    (
        select yhh, max(gdtime) as gdtime
        from table1 
        group by yhh
    ) x
    where x.yhh = table1.yhh 
      and x.gdtime = table1.gdtime
)

/* 2 用窗口函数 */
select * from
(
select yhh, name, gdcs, gdsj1, gdtime
, max(gdtime) over(partition by yhh) as gdtimeMax
from table1
) x
where gdtime = gdtimeMax

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧