[英]MySQL: problem with SELECT SUM()
my query: 我的查询:
SELECT events.*, SUM(analyt_event_hits.ajax_hits) AS ajax_hits
FROM events
LEFT JOIN analyt_event_hits
ON events.event_id = analyt_event_hits.event_id
WHERE events.date >= '$d'
the problem: 问题:
this is only returning one result, when it should be returning many.
这只会返回一个结果,而应该返回多个结果。
The query works fine if i remove SUM(anal_event_hits.ajax_hits) AS ajax_hits
如果我删除
SUM(anal_event_hits.ajax_hits) AS ajax_hits
,查询工作正常
I'm a bit of a MySQL novice so hopefully i'm missing something obvious! 我是MySQL的新手,所以希望我缺少一些明显的东西!
If you use a group function like SUM
in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows . 如果在不包含GROUP BY子句的语句中使用
SUM
类的组函数, 则等效于对所有行进行分组 。
and That's why you get only one row in results 这就是为什么您只获得一行结果的原因
More details on Aggregate Functions : 有关聚合函数的更多详细信息:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Try adding 尝试添加
GROUP BY events.event_id
to the end. 到最后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.