title: Select From One Table And Include Sum Value From Another
body: <p>My title is terrible but I'm having a hard time wrapping my head around this.  I have two tables.  Links and Votes.  Links contain info about links submitted and the usual jazz, votes contains a foreign key link_id and a karma_up and karma_down value (both unsigned ints).  Because I want to limit votes to one per user and record vote time I opted for the two tables method.  What I want to do though is display the sum'ed karma of a link.  I need to be able to grab all the links and then their one to many votes which are sum'ed up SUM(karma_up - karma_down).  All I have been able to do is SUM up the entire column which doesn't do me any good.</p>
  
  <p>Hopefully someone just "gets" what I'm trying to do but till then I will be updating this post with as much relevant information as possible.</p>
  
  <p><strong>UPDATE</strong>
  For anyone interested, my final query is</p>
  
  <pre><code>SELECT links.*, (SUM(karma_up) - SUM(karma_down)) AS karma
  FROM links, votes
  WHERE links.id = votes.link_id
  GROUP BY votes.link_id
  </code></pre>