Page 1 of 1

SUM() over 2 tables using JOIN

Posted: Thu Apr 06, 2006 4:01 pm
by visionmaster
Hello together,

I have 2 tables holding statistical data. The clicks (field "homepage_click") in both tables concerning the company with the id=1 should be summed up to one value.

Table clicks:
id_firmendaten homepage_click
1 5

Table clicks_month:
id_firmendaten homepage_click
1 3
1 1

This is my MySQL-statement:

Code: Select all

SELECT SUM( t1.homepage_click + t2.homepage_click ) AS homepage_click
FROM clicks AS t1
JOIN clicks_month AS t2 ON t1.id_firmendaten = t2.id_firmendaten
WHERE t1.id_firmendaten =1
=> homepage_click = 14

That is obviously _incorrect_, here homepage_click = 9 would be the correct result.

What am I doing wrong? I would like to avoid any PHP coding and have MySQL do the addition of the values in both tables.


Thanks for your help,
visionmaster