title: ON DUPLICATE KEY UPDATE - Condition WHERE vs CASE WHEN vs IF?
body: <p>I am refering to <a href="https://stackoverflow.com/questions/35529809/php-update-sql-statement-with-multiples-rows/35530384#35530384">this post</a>. I am stuck with a problem I can't resolve. I try to insert multiple rows with a php script into a MySQL database. I don't succeed in updating the whole thing using ON DUPLICATE KEY UPDATE and using a WHERE condition (at the end of the code below) I would like to use to update only an entry has been modified recently:</p>
  
  <pre><code>// for information (used in a foreach loop):
  $args[] = '("'.$row['lastname'].'", '.$row['phone'].', "'.$row['lastModification'].'")';
  
  // then:
  $stringImplode = implode(',', $args);
  
  // Where I am stuck - WHERE statement:
  $sql = $mysqli-&gt;query('INSERT INTO table_name '. (lastname, phone, timestamp) .' VALUES '.$stringImplode .'ON DUPLICATE KEY UPDATE lastname=VALUES(lastname), phone=VALUES(phone) WHERE timestamp &gt; VALUES(lastModification);
  </code></pre>
  
  <p>Everything works fine except I cannot set any WHERE condition at this point that involves multiples entries. Maybe the WHERE statement in this case is not intended to refer to a condition in this statement.</p>
  
  <p>I was told to try with a database procedure using a JOIN statement and a temporary table with first all my entries and then querying some conditions. But I have to admit I don't understand very well how I could leverage such a table to update an other table.</p>
  
  <p>Is there an easy and lovely way to use a <strong><code>"CASE WHEN"</code></strong> or an <strong><code>"IF" statement</code></strong> in this case?</p>
  
  <p>Would something like</p>
  
  <pre><code>INSERT INTO ... ON KEY DUPLICATE UPDATE lastname = VALUES(lastname), phone = VALUES(phone) 
  CASE WHEN (timestamp &gt; VALUES(lastModification)) THEN do nothing ...
  </code></pre>
  
  <p>or</p>
  
  <pre><code>...ON KEY DUPLICATE UPDATE... IF (timestamp &gt; VALUES(lastModification)) ...
  </code></pre>
  
  <p>If anyone could help me, I would be very grateful.</p>
  
  <p><strong>EDIT: Since I will have many variables, could it be used in this way:</strong></p>
  
  <pre><code>INSERT INTO ... ON KEY DUPLICATE UPDATE
  
  IF(timestamp &gt; VALUES(timestamp),
      (
      name = VALUES(name),
      number = VALUES(number),
      timestamp = VALUES(timestamp)
      ....many other variables
      ),
      (
      name = name,
      number = number,
      timestamp = timestamp
      ....many other variables)
      )
  </code></pre>