SUM() over 2 tables using JOIN

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

SUM() over 2 tables using JOIN

Post 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
Post Reply