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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With