title: MySQL IF statement in UPDATE query
body: <p>Why cannot i use IF like this</p>
  
  <pre><code>UPDATE my_users
      IF(position2 = 18, "SET position = 17", ''),
      IF(position2 = 17, "SET position = 16", ''),
      IF(position2 = 16, "SET position = 15", ''),
          WHERE user_id =&gt; 170 AND user_id &lt;= 1000
  </code></pre>
  
  <p>Only thing that works is</p>
  
  <pre><code>UPDATE my_users
      SET position2 = IF(position2 = 18, 17, 
                        IF(position2 = 17, 16, 
                          IF(position2 = 16, 15, ''
                        )
                          )
                            )
          WHERE user_id =&gt; 170 AND user_id &lt;= 1000
  </code></pre>
  
  <p>is there way to make same thng in any other way because i have more than 40 conditions for that field position2 and its easy to get lost in this way?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>If i run </p>
  
  <pre><code>UPDATE my_users
      SET position2 = CASE position2  WHEN 18 THEN 17
                      WHEN 17 THEN 16
                      WHEN 16 THEN 15
                      END
              WHERE user_id =&gt; 170 AND user_id &lt;= 1000
  </code></pre>
  
  <p>when <code>position2</code> is anything not in CASE statement it sets it to nothing, how can i just keep any value other than what in CASE statement the same</p>
  
  <p><strong>Edit 2:</strong>
  I guess solution is:</p>
  
  <pre><code>   UPDATE my_users
          SET position2 = CASE position2  WHEN 18 THEN 17
                          WHEN 17 THEN 16
                          WHEN 16 THEN 15
                              ELSE position2
                          END
                  WHERE user_id =&gt; 170 AND user_id &lt;= 1000
  </code></pre>
  
  <p><strong>Edit 3:</strong>
  If my request was not one time thing the more faster request would be as suggested Vatev</p>
  
  <pre><code>   UPDATE my_users
          SET position2 = CASE position2  WHEN 18 THEN 17
                          WHEN 17 THEN 16
                          WHEN 16 THEN 15
                          END
                  WHERE user_id =&gt; 170 AND user_id &lt;= 1000
                  AND position2 &gt;= 16 and &lt;= 18
  </code></pre>