title: mysql sum multiple joins
body: <p>I'm trying to write a <code>SELECT</code> query that joins multiple one-to-many tables. A table named <strong>disbursements</strong> has both a  <code>LEFT JOIN</code>  to a table named <strong>contacts</strong> (because a contact can have many disbursements) and a  <code>LEFT JOIN</code>  to a table named <strong>pledges</strong> (because a pledge (promise of payment) can have multiple disbursements (actual payments).</p>
  
  <p>My question is how can I pull records that list contacts, their pledges, and the pledge balance (the sum of disbursements towards a pledge subtracted from the pledge)?</p>
  
  <p>Here is what I got so far:</p>
  
  <pre><code>SELECT *, (p.pl_amount - SUM(disb_amount)) as balance FROM disbursements d
  LEFT JOIN contacts c on c.c_no = d.c_no
  LEFT JOIN  pledges p on d.pl_no = p.pl_no 
  GROUP BY d.disb_no
  HAVING balance &gt; 500
  ORDER BY c.c_last
  </code></pre>
  
  <p>Thank you in advance</p>