此查询:
SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count >1
返回大约1500(我正在查找的数字)结果,只有count字段。我怎么能还返回所有计数字段的总和?当我尝试
SELECT COUNT(source) AS count,
SUM(count) as total
FROM call_details
GROUP BY source
HAVING count >1
我在'字段列表'错误中收到'未知列'计数'。
和
SELECT COUNT(source) AS count,
SUM(COUNT(source)) as total
FROM call_details
GROUP BY source
HAVING count >1
给我一个'无效使用群组功能'
有什么想法吗?我可以做第一组mysql_num_rows($result)
(获取我需要的信息),但我真的想通过MySQL来做。
答案 0 :(得分:36)
SELECT COUNT(count) FROM (SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count > 1) as A
答案 1 :(得分:15)
您无法在行上下文中获得全局总计。在COUNT()在任何特定行上完成时,没有什么要SUM,因为还没有计算其他行。
您必须首先运行SUM查询以获取您的个人统计信息,然后在脚本中手动汇总,或者使用周围的SUM子句重新运行查询:
SELECT SUM(count) FROM (
SELECT original query here...
)
答案 2 :(得分:2)
假设您要在应用程序中获取所有结果,我认为最有效的方法是在应用程序代码中总结它。
答案 3 :(得分:1)
试试这个
select mycount, sum(mycount) as sumcount
from
(SELECT COUNT(source) AS mycount FROM call_details GROUP BY source HAVING mycount >1) counttable
答案 4 :(得分:1)
只需删除选择查询中的“分组依据”子句
即可# first, get your counts by source
SELECT COUNT(source) AS count
FROM call_details
GROUP BY source
HAVING count >1
# then, get the overall total
SELECT COUNT(source) AS count
FROM call_details
HAVING count >1