Selecta. msg_id, a.com _ id, a. data, a. ctimefromsns_user_03.user_request_86awhere5 (selectcount (*) timeout) orderbya. ctime; the preceding SQL statement implements group query,
Select. msg_id, a.com _ id,. data,. ctime from sns_user_03.user_request_86 a where 5 (select count (*) from sns_user_03.user_request_86 where uid = 8880386 and com_id =. app_id and msg_id. msg_id) order by. ctime; the preceding SQL statement implements grouping query,
Select. msg_id, a.com _ id,. data,. ctime from sns_user_03.user_request_86 a where 5> (select count (*) from sns_user_03.user_request_86 where uid = 8880386 and com_id =. app_id and msg_id>. msg_id) order by. 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.