table: countries_total
site_id country_id hits
-------------------------------------
1 1 122
1 2 110
1 3 103
table raw_requests
site_id country_id hit (1=yes. 0=no)
--------------------------------
1 1 1
1 2 1
1 2 1
1 2 0
1 3 1
I need to UPDATE countries_total with raw_requests.
So after the UPDATE query runs table countries_total would look like:
site_id country_id hits
-------------------------------------
1 1 123 (added 1)
1 2 112 (added 2)
1 3 104 (added 1)
I know you can do INSERT... SELECT but you can't do UPDATE... SELECT.
Can this be done using a sub query or something?
All I could come up with is:
UPDATE countries_total,
raw_requests SET countries_total.hits = countries_total.hits + SUM( raw_requests.hit ) WHERE countries_total.country_id = raw_requests.country_id
MySQL complains saying invalid use of group by function (SUM)