title: Counting instances in table
body: <p>I've got this tag system for tagging blog entries and such. The tags are in one table, containing only a tag name and a primary key. Then I have another table with objects that are using the tags.</p>
  
  <p>It could look something like this:</p>
  
  <pre><code>_________________________________
  | tags                          |
  --------------------------------|
  | id  | name                    |
  |-------------------------------|
  |  1  | Scuba diving            |
  |  2  | Dancing                 |
  ---------------------------------
  
  _________________________________
  | tag_objects                   |
  --------------------------------|
  | id  | tag  | object           |
  |-------------------------------|
  |  1  |  2   | 13               |
  |  2  |  2   | 18               |
  |  3  |  1   | 24               |
  ---------------------------------
  </code></pre>
  
  <p>Now, what I need to accomplish is to to add a column to the tags table, called "occurrences" or something. For each tag in tags, occurrences should be set to the number of times that tag is used in tag_objects.</p>
  
  <p>So basically something like (obviously pseudo-code):</p>
  
  <pre><code>foreach(tags):
      UPDATE tags
         SET occurrences = (SELECT COUNT(id) 
                              FROM tag_objects 
                             WHERE tag = tags.id);
  </code></pre>
  
  <p>When people create new posts and stuff in the future, I'll just have a trigger to update the count, but I have a couple of thousand rows already that I need to count first. I don't know how to do this, so any assistance would be appreciated.</p>