tags:

views:

25

answers:

2

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!

A: 

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

Haim Evgi
ah, thanks, will read!
Haroldo
A: 

Try adding

GROUP BY events.event_id

to the end.

beamrider9
that's brilliant - thank you so much
Haroldo