SELECT max(login_time) ,user_id ,browser from tb_system_user_login_log group by user_id 我本地有个类似表,好像这样是可以的。
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;
把用户的名字和发帖时间作为一个联合主键来处理。
daemonghost
2017-08-11 16:21:14 +08:00
先按用户名分组,然后再按日期排序,选取指定个数就行了
beginor
2017-08-11 16:39:31 +08:00
最佳的方案是 `row_number` + `partition` , 但是 MySQL 不支持
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 );
不要用聚合什么的
lenmore
2017-08-12 10:34:29 +08:00
终极做法是: 当用户发帖时,将最新的 ID 记录到用户表或者单独的一张表。更极端一点把标题时间都冗余了。 查询时 Join 帖子。
tsotsi
2017-08-12 15:06:18 +08:00
```sql select *,if(@lastOne=userName,'',@lastOne:=userName) flag from (select * from table order by userName,postDate desc) a,(select @lastOne :='')b where flag <> '' ```