Select a. msg_id, a.com _ id, A. Data, A. ctime from sns_user_03.user_request_86 A where 5> (select count (*) from sns_user_03.user_request_86 where uid = 8880386 andCom_id= A. app_id and msg_id> A. msg_id) order by A. ctime;
In the preceding SQL grouping query, each group can display up to five records, sorted by Time
Select @ rank: = 0;
Select msg_id, app_id, Data, ctime, rank from (select a. msg_id, A. app_id, A. Data, A. ctime, if (@ appid! =. App_id, @ rank: = @ rank + 1, @ rank: = @ rank) as rank, @ appid: =. app_id from sns_user_03.user_request_86 A where 5> (select count (*) from sns_user_03.user_request_86 where uid = 8880386 and app_id =. app_id and msg_id>. msg_id) and @ rank <= 4 order by. msg_id DESC) 'temp 'where rank> 2 and rank <= 4;
In the preceding example, 3rd to 4th groups are obtained, with 5 Records in each group sorted by time.
This is only a pure dB implementation. In practical applications, we should avoid such time-consuming dB operations as much as possible. It is best to cache the data and perform such complex operations at the logic layer.