title: INSERT IGNORE INTO and UPDATE in one statement
body: <p>I'm running a script which serves downloads to my users. I would like to monitor their traffic on a per byte level, and I hold how many bytes they've downloaded in <code>$bytes</code>. I want to log it to my database and I'm using the following function:</p>
  
  <pre><code>register_shutdown_function(function() {
      global $bytes;
  
      /* Save the traffic to the database */
      $db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
      $st = $db-&gt;prepare('INSERT IGNORE INTO `stats`
                          SET `date` = CURDATE(), `bytes` = :bytes');
      $st-&gt;bindParam(':bytes', $bytes);
      $st-&gt;execute();
      $st = null;
      $db = null;
  });
  </code></pre>
  
  <p>This query seems to work once, when the download is complete there's a new record in the table with the following data:</p>
  
  <pre><code>   date      |    bytes
  ------------------------
  2013-02-03   |   2799469
  </code></pre>
  
  <p>however, on every other download the <code>bytes</code> field doesn't change. No new records and no change to the record that's already in the table. It's pretty obvious what the problem is, the query tries to insert a record but if it already exists then it aborts. I need an update statement like this:</p>
  
  <pre><code>UPDATE `stats`
  SET `bytes` = `bytes` + :bytes
  WHERE `date` = CURDATE()
  </code></pre>
  
  <p>but I would like to do the entire operation in one query. A query that will create the record if it doesn't exist and if it does exist, update the existing record.</p>
  
  <p>Can this be done or am I going to have to run two queries on every download?</p>