![]() | 1 daimazha 2017-08-11 09:52:34 +08:00 给你个思路: select * from x where id in ( select max(id) from x group by name); |
![]() | 6 LeeSeoung 2017-08-11 10:11:04 +08:00 ![]() select distinct id,userName,post,max(postDate)over(partition by id,userName,post) from xxx |
![]() | 7 LeeSeoung 2017-08-11 10:11:52 +08:00 前提 postDate 得是 date 类型的,不是的话自己转换下 |
![]() | 8 ayumilove 2017-08-11 10:12:27 +08:00 子查询结果比较大,建议用 exists |
![]() | 9 ayumilove 2017-08-11 10:14:43 +08:00 要是 Oracle 6 楼 的好~ |
![]() | 10 daimazha 2017-08-11 10:28:11 +08:00 |
![]() | 11 babywhisper 2017-08-11 10:28:28 +08:00 select * from (select * from <table> ORDER BY archiveTime DESC) as tmp GROUP BY tmp.userName 这样也行把, 但不知道效率如何. |
![]() | 13 babywhisper 2017-08-11 10:35:56 +08:00 如果有用**Django ORM**的同学可以参考这篇: https://changchen.me/blog/20170515/ele-interview-solution/ 用 annotate 实现楼主想要的效果 ^_^ |
![]() | 14 nullcc 2017-08-11 10:42:52 +08:00 select A.userName, postDate, post from table A inner join (select distinct userName, max(postDate) as maxPostDate from table group by userName) B on A.userName = b.userName and A.postDate = B.maxPostDate 大概是这样 |
![]() | 15 anoymoux 2017-08-11 10:50:45 +08:00 加一张表 latest_post(userid,postid) |
![]() | 17 quickma 2017-08-11 11:47:20 +08:00 加表是一个不错的想法,view 也行呀。 |
![]() | 18 noNOno 2017-08-11 11:55:03 +08:00 select * from (select *,row_number over (partition by userName order by postdate desc ) as rn from table ) where rn=1 能用 rownumber 吧 |
19 syncher 2017-08-11 11:59:51 +08:00 via Android 又是一个 group |
![]() | 20 fxxkgw 2017-08-11 12:44:17 +08:00 order by 加 group by |
![]() | 21 jeffpan 2017-08-11 14:58:20 +08:00 SELECT max(login_time) ,user_id ,browser from tb_system_user_login_log group by user_id 我本地有个类似表,好像这样是可以的。 |
22 Buffer2Disk 2017-08-11 15:02:30 +08:00 @daimazha 的思路不错,但是如果 id 不是自增的,而是通过 uuid 生成的,这个时候下面这个方法可以解决问题。 select b.* from ( select max(post_date) as post_date, user_name from article group by user_name) as a inner join article b ON a.post_date = b.post_date AND a.user_name = b.user_name; 把用户的名字和发帖时间作为一个联合主键来处理。 |
![]() | 23 daemonghost 2017-08-11 16:21:14 +08:00 via Android 先按用户名分组,然后再按日期排序,选取指定个数就行了 |
![]() | 24 beginor 2017-08-11 16:39:31 +08:00 via Android 最佳的方案是 `row_number` + `partition` , 但是 MySQL 不支持 |
25 finull 2017-08-11 21:09:55 +08:00 select * from table t where not exists ( select * from table where userName = t.userName and postDate > t.postDate ); 不要用聚合什么的 |
![]() | 26 lenmore 2017-08-12 10:34:29 +08:00 终极做法是: 当用户发帖时,将最新的 ID 记录到用户表或者单独的一张表。更极端一点把标题时间都冗余了。 查询时 Join 帖子。 |