MySQL计数超出限制 [英] MySQL count all outside limit

查看:104
本文介绍了MySQL计数超出限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL查询,该查询计算了我们从前五名客户那里获得的工作时间.这工作正常,我正在将数据绘制在图形上.我要实现的是查询中的计算,该计算返回第六个结果集,该结果集是所有小时数的总和,而不是前五名.即其他"结果,因此所有6个结果的总和等于项目总小时数.

I have a MySQL query which counts hours we have worked from the top five of our clients. This works fine, and I am plotting the data on a graph. What I am trying to achieve is a calculation within the query that returns a sixth result set which is the sum of all the hours which are not in the top five. i.e. An 'Other' result, so that the sum of all 6 results would equal the total of sum of projects.hours.

我的查询:

SELECT
  SUM(projects.hours) AS total_hours
FROM projects
GROUP BY projects.companyID
ORDER BY total_hours DESC
LIMIT 5

推荐答案

SELECT
SUM(projects.hours) AS total_hours,
(SELECT SUM(projects.hours) FROM projects) AS all_hours
FROM 
projects
GROUP BY 
projects.companyID
ORDER BY total_hours DESC
LIMIT 5

从应用程序的所有小时中减去总小时.

Subtract total_hours from all_hours in your application.

这篇关于MySQL计数超出限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆