title: SQLite/MySQL replace only if timestamp of row is newer
body: <p>I'm working on a sync engine and am stuck on a problem:</p>
  
  <p>I need to <strong>bulk INSERT OR REPLACE</strong> multiple items in one query <strong>but only if the timestamp 'lastModified' of the item is newer</strong> than it's current value in the database (if the item exists - if the item doesn't exist yet in the db, then it just should be inserted).</p>
  
  <p>What I've come up with so far is this:</p>
  
  <pre><code>REPLACE INTO tableName (id, name, lastModified) VALUES
  CASE WHEN lastModified &lt; [itemLastModified] THEN ([itemId], [itemName], [itemLastModified]), END
  CASE WHEN lastModified &lt; [itemLastModified] THEN ([itemId], [itemName], [itemLastModified]), END
  CASE WHEN lastModified &lt; [itemLastModified] THEN ([itemId], [itemName], [itemLastModified]) END
  </code></pre>
  
  <p>but that won't work obviously, since each case statement searches all rows in the table and not concentrates on the current item. Further more nothing would happen if the item didn't exists yet, I guess...</p>
  
  <p>Does anyone know a good way to solve that problem?</p>