mysql group by 如何解决聊天分组 取最新记录

数据结构

id sendId receiverId      postDate
1     1            2         2015-04-06 12:12:01
2     1            3         2015-04-06 12:13:01
3     2            1         2015-04-06 12:14:01
4     2            3         2015-04-06 12:15:01
5     3            2         2015-04-06 12:16:01
6     4            1         2015-04-06 12:17:01
7     1            4         2015-04-06 12:18:01
8     2            4         2015-04-06 12:19:01

意思就是 按照( 1-2 ,2-1 )(1-3, 3-1) (1-4 ,4-1)( 2-4 ,4-2)
这样分组,并且取分组最新的数据,为了实现聊天记录的获取。

阅读 6.1k
2 个回答

假定你表名为message

select * from message where (select count(*) from message as m where m.sendId = message.sendId and m.postDate >= message.postDate ) < 2

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题