9

Why cannot i use IF like this

UPDATE my_users
    IF(position2 = 18, "SET position = 17", ''),
    IF(position2 = 17, "SET position = 16", ''),
    IF(position2 = 16, "SET position = 15", ''),
        WHERE user_id => 170 AND user_id <= 1000

Only thing that works is

UPDATE my_users
    SET position2 = IF(position2 = 18, 17, 
                      IF(position2 = 17, 16, 
                        IF(position2 = 16, 15, ''
                      )
                        )
                          )
        WHERE user_id => 170 AND user_id <= 1000

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?

Edit:

If i run

UPDATE my_users
    SET position2 = CASE position2  WHEN 18 THEN 17
                    WHEN 17 THEN 16
                    WHEN 16 THEN 15
                    END
            WHERE user_id => 170 AND user_id <= 1000

when position2 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

Edit 2: I guess solution is:

   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 => 170 AND user_id <= 1000

Edit 3: If my request was not one time thing the more faster request would be as suggested Vatev

   UPDATE my_users
        SET position2 = CASE position2  WHEN 18 THEN 17
                        WHEN 17 THEN 16
                        WHEN 16 THEN 15
                        END
                WHERE user_id => 170 AND user_id <= 1000
                AND position2 >= 16 and <= 18
9
  • 2
    Take a look at CASE. Commented Jul 27, 2012 at 23:58
  • 1
    Btw if you want to decrement the column you can just SET position2 = position2-1;. Commented Jul 28, 2012 at 0:01
  • One more question i added to main post Commented Jul 28, 2012 at 0:48
  • 1
    just add ELSE clause, like ... ELSE position2 END. Or just update only those rows where position2 IN (16,17,18) with position2-1, it'd be better. Commented Jul 28, 2012 at 0:54
  • The proper way would be to add AND position2 IN (16,17,18) to your WHERE clause. You can also put a 'default' CASE like this CASE position2 WHEN .. THEN .. ELSE position2 END. Commented Jul 28, 2012 at 0:54

2 Answers 2

4

You can also do something like this:

                UPDATE my_users
                     SET position2 = CASE WHEN position2 =18 THEN 17
                        WHEN position2 =17 THEN 16
                        WHEN position2 =16 THEN 15 ELSE position2 
                        END
                WHERE user_id between 170 AND  1000
                AND position2 between 16 and  18
Sign up to request clarification or add additional context in comments.

Comments

2

This is how I would rewrite your query:

UPDATE
  my_users
SET 
  position2 = position2 - 1
WHERE
  position2 >= 16
  AND position2 <= 18
  AND user_id >= 170 
  AND user_id <= 1000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.