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
I'm a bit of a MySQL novice so hopefully i'm missing something obvious!
Try adding
GROUP BY events.event_id
to the end.
If you use a group function like SUM
in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
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
精彩评论