知识管理反馈系统中,分析知识检索的办公室(北京上海天津香港纽约东京....苏州)来源,我需要查询哪些办公室次数较多,排出前5个,并计算出剩余所有部门的查询次数之和。
数据库是mysql。
1 拼出前五个
select sum(count) as total,location from tb_person group by location limit 5;
2.检索其他所有
理论上应该是:
select ‘其它‘,sum(count) from tb_person where location not in (select location from tb_person group by location limit 5);
但是sql无法执行,提示:
This version of MySQL doesn‘t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery
子查询中无法使用limit,检索到解决方案是在此再套一层查询,拼出来:
select ‘其它‘,sum(count) from tb_person where location not in (select t.location from (select location from tb_person group by location limit 5) as t);
成功得到结果~