开发者社区 问答 正文

mysql对分组进行过滤,查找最新数据的问题

id uid fid mes h_date
1 1 2 gge 2013-01-04 16:07:36
2 1 3 agegg 2013-01-03 16:07:36
3 1 2 agea 2013-01-05 16:07:36
4 2 3 gggg 2013-01-09 16:07:36
5 1 3 gggee 2013-01-07 16:07:36
uid 是自己的id,fid是对方的id,mes是消息,h_date是时间
现在想做的就是根据fid分组,查找uid为1 的最新的数据。不能重复。
结果是id为3和5的这两条数据

展开
收起
落地花开啦 2016-02-12 13:44:42 2279 分享 版权
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

    仅供参考,你试下下面的代码
    `select t.* from table as t ,(
    select max(id) as mid from table where uid=1 group by fid) as t2
    where t.id=t2.mid; `

    2019-07-17 18:41:18
    赞同 展开评论