title: query to update records count based on relation and constraints
body: <p>Im trying to update a column <code>(address_count)</code> on my <code>people</code> table that must have the sum of records from another table <code>(people_addresses)</code> plus some constraints.</p>
  
  <p>The idea is that, the record on <code>people</code> must exist on <code>people_addresses</code> and <em>must be</em> present on at least one of the other tables (table a, table b)</p>
  
  <pre><code>SELECT pd.address_id, count (pd.person_id)
  FROM person_addresses pd
  WHERE EXISTS (SELECT a.person_id 
                FROM table_a a where a.person_id = pd.person_id)
    OR EXISTS (SELECT b.person_id 
               FROM table_b b where b.person_id = pd.person_id)
  GROUP BY pd.address_id
  </code></pre>
  
  <p>With the query above, I get the expected count, for each address that fits my requirements, altough, when I try to use it on a update, I get weird results, the records that don't fit the requirements end up with wrong values on <code>people_count</code> </p>
  
  <p>query im using:</p>
  
  <pre><code>UPDATE people d
  SET people_count = (SELECT count (pd.person_id) 
                      FROM person_addresses pd
                      WHERE EXISTS (SELECT a.person_id 
                                    FROM table_a a where a.person_id = pd.person_id)
    OR EXISTS (SELECT b.person_id 
               FROM table_b b where b.person_id = pd.person_id)
    AND d.id=pd.address_id
    GROUP BY pd.address_id) 
  </code></pre>
  
  <p>I get the correct result, on records that fit the requirements, but wrong values on the ones that dont fit, when I run the update.</p>