开发者

MySQL: problem with SELECT SUM()

开发者 https://www.devze.com 2023-01-09 14:09 出处:网络
my query: SELECT events.*, SUM(analyt_event_hits.ajax_hits) AS ajax_hits FROM events LEFT JOIN analyt_event_hits

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

0

精彩评论

暂无评论...
验证码 换一张
取 消