Somewhat lengthy sum() mysql troubles..

admin

Administrator
Staff member
You will have to excuse the snacks in the sample data, I am a bit hungry. :)

Sample Table 1: terms.*

id term
1 willy wonka
1 butterfinger
1 milkyway
1 milk duds
1 snickers
1 goober
2 chips
2 nachos
3 soda
4 whatever

Sample table 2: clickdata.*

id term date login clicks
1 willy wonka 2001-02-02 bob 1
1 butterfinger 2001-02-02 bob 3
1 milkyway 2001-02-03 bob 4
1 willy wonka 2001-02-03 bob 1
1 butterfinger 2001-02-03 bob 3
1 milkyway 2001-02-02 bob 4
1 milk duds 2001-02-02 bob 4
1 snickers 2001-02-02 bob 2
1 goober 2001-02-02 bob 6
2 chips 2001-02-02 bob 2
2 nachos 2001-02-02 bob 0
3 soda 2001-02-02 joe 2
4 whatever 2001-02-02 joe 2

The vitals:
mySQL 3.23.33
OS RH6.2


SELECT terms.id, terms.term, sum(hitout)
FROM terms, clickdata
WHERE terms.id=clickdata.id AND clickdata.login='bob'
GROUP BY terms.term ORDER BY term;

In theory I am shooting for:

id term sum(clicks)
1 willy wonka 2
1 butterfinger 6
1 milkyway 8
1 milk duds 4
1 snickers 2
1 goober 6
2 chips 2
2 nachos 0

However, my results are as followed:

id term sum(clicks)
1 willy wonka 20
1 butterfinger 20
1 milkyway 20
1 milk duds 20
1 snickers 20
1 goober 40
2 chips 20
2 nachos 5

I have used the sum in the past and have never had a problem, I just can not figure out what I am doing wrong in this case.

Thanks for any help you can offer!
 
Back
Top