详解MySQL分组排序求Top N
MySQL分组排序求Top N
表结构
按照grp分组,按照num排序,每组取Top 3,输出结果如下:
源代码:
SELECT * FROM score AS t3 WHERE ( SELECT COUNT(*) FROM score AS t1 LEFT JOIN score AS t2 ON t1.grp = t2.grp AND t1.num < t2.num WHERE t1.id = t3.id ) < 3 ORDER BY t3.grp ASC, num DESC
在where中可以通过子查询创造一个新的变量来过滤。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关推荐
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11