title: Mysql Trigger to INSERT a record or UPDATE if it already exists?
body: <p>I have 2 tables 
  1st table word_cloud (auto inc), predefined_text, content_id .
  2nd table word_cloud_count (auto inc),predefined_text,content_id,count .
  I am calling a trigger when data is inserted into word_cloud, which basically groups the word, calculates the count and inserts it into word_cloud_count</p>
  
  <p>word_cloud table consists of </p>
  
  <pre><code>id | predefined_text | content_id
  ---------------------------------
   1 | ABC             | 1
   2 | ABC             | 1
   3 | ABC             | 1
   4 | XYZ             | 2
   5 | XYZ             | 2
   6 | XYZ             | 2
   7 | XYZ             | 1
   8 | ABC             | 2
   9 | ABC             | 2
  10 | ABC             | 2
  </code></pre>
  
  <p>word_cloud_count consists of </p>
  
  <pre><code>id | predefined_text | content_id | count
  -----------------------------------------------
   1 | ABC             | 1          |  3
   2 | XYZ             | 2          |  3
   3 | ABC             | 2          |  3
   4 | XYZ             | 1          |  1
  </code></pre>
  
  <p><strong>Trigger Function</strong></p>
  
  <pre><code>CREATE TRIGGER word_cloud_count_insert after insert on word_cloud
      INSERT INTO word_cloud_count (Word,content_id ,count )
      SELECT predefined_text,content_id, count(*)
      FROM word_cloud
      group by predefined_text,content_id
  </code></pre>
  
  <p>Everything works fine and the values are being inserted in the word_cloud_count table </p>
  
  <p>However my problem is every time the data is inserted into word_cloud the trigger is called and duplicate values are being stored into word_cloud_count </p>
  
  <p>How do i avoid this</p>
  
  <p>Any suggestions would be of great Help, Thanks in advance</p>