123

mysql 查询每个分组的前几名

 

 

按分组排序,并查出每个分组的前3名

 单表

 

SELECT
    * 
FROM
    (
SELECT
    ZONEID,
    uid,
    NAME,
    fight,
IF
    ( @zone = zoneid, @rank := @rank + 1, @rank := 1 ) AS rank,
    ( @zone := zoneid ) AS zone 
FROM
    `user` u,
    ( SELECT @rank := 0 ) t,
    ( SELECT @zone := 1 ) z 
ORDER BY
    zoneid DESC,
    fight DESC 
    ) a 
WHERE
    rank <= 3 
ORDER BY
    zoneid DESC,
    rank DESC

 

posted on 2019-10-26 10:41  J依情  阅读(1350)  评论(0编辑  收藏  举报

导航