title: MySQL How to insert new record or update a field depending on whether it exists?
body: <p>I am trying to implement a rating system where I keep the following two fields in my db table:</p>
  
  <p>rating (the current rating)
  num_rates (the number of ratings submitted so far)</p>
  
  <pre><code>UPDATE `mytable`
     SET rating=((rating*num_rates)+$theRating)/num_rates, num_rates=num_rates+1
   WHERE uniqueCol='$uniqueCol'
  </code></pre>
  
  <p>the variables are from my PHP code.</p>
  
  <p>So, basically sometimes the row with the uniqueCol does not exist in the DB, so how can I do the above statement if the exists and if it doesn't then do something like this:</p>
  
  <pre><code>INSERT INTO `mytable`
     SET rating=$theRating, num_rates=1, uniqueCol=$uniqueCol
  </code></pre>