title: SQL join one to many relationship - count number of votes per image?
body: <p>Okay, so I have 2 tables:</p>
  
  <pre><code>images                           votes
  ----------------------------     --------------------
  image_id |  name | square_id     vote_id |  image_id
  ----------------------------     --------------------
  1         someImg    14          1            45  
  2         newImg     3           2            18     
  3         blandImg   76          3            1 
  ...                              ...
  n                                n
  </code></pre>
  
  <p>This is a one to many relationship. Each image can have multiple votes, but a vote can only be related to one image. I'm trying to produce a join query which will show the image id, and the number of votes it has under a specified condition (say, based on <code>square_id</code>). Thus the query result would look similar to this:</p>
  
  <pre><code>query_result
  ----------------------
  image_id |  vote_count
  ----------------------
  18          46
  26          32
  20          18
  ...
  55          1
  </code></pre>
  
  <p>But the best I can do is this:</p>
  
  <pre><code>query_result
  ----------------------
  image_id |  vote_id
  ----------------------
  18          46
  18          45
  18          127
  26          66
  26          43
  55          1
  </code></pre>
  
  <p>See the problem? Each <code>image_id</code> is listed multiple times for each <code>vote_id</code> it has. This is the query which produces this:</p>
  
  <pre><code>SELECT images.image_id, votes.vote_id 
    FROM votes JOIN images ON images.image_id=votes.image_id
  </code></pre>
  
  <p>I just can't seem to create a <code>vote_count</code> column which is the sum of all the votes that image has. Is there some way that I can use the <code>count()</code> function to do so, that I'm simply not aware of? </p>