原语句:
select *
from (select *
from rating
order by rat_date_time desc)
group by app_id
修改后:
select r.* from (
select @rownum := case when @gapp_id = app_id then @rownum + 1 else 1 end as rownum, id, @gapp_id := app_id
from app_rating, (select @rownum := 0, @gapp_id = '') t
order by app_id, rat_date_time desc
) t1 inner join app_rating r on r.id = t1.id
where rownum = 1
注意点:
若需要分页,不能直接用PageHelper.startPage(pageNo, pageSize),因为获取分页总数时会忽略order by
select count(*)
from (select r.* from (
select @rownum := case when @gapp_id = app_id then @rownum + 1 else 1 end as rownum, id, @gapp_id := app_id
from app_rating, (select @rownum := 0, @gapp_id = '') t
) t1 inner join app_rating r on r.id = t1.id
where rownum = 1
)
故,需要重新获取总数的count方法。
在屏蔽分页取总数后,手动设置total。
PageHelper.startPage(pageNo, pageSize, false)