码迷,mamicode.com
首页 > 数据库 > 详细

SQL查询前五条数据及其他所有数据之和

时间:2014-08-27 16:12:37      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   io   数据   div   log   amp   

知识管理反馈系统中,分析知识检索的办公室(北京上海天津香港纽约东京....苏州)来源,我需要查询哪些办公室次数较多,排出前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);


成功得到结果~

 

SQL查询前五条数据及其他所有数据之和

标签:style   blog   color   使用   io   数据   div   log   amp   

原文地址:http://www.cnblogs.com/valentineisme/p/3939541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!