0

i want to minus 1 (-1) value from durasi_pre_u if durasi_pre_u have basic value bigger then 0 and update Premium to Member when durasi_pre_u have value = 0.

here what i try

UPDATE user 
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u), 
pangkat_u = IF(durasi_pre_u <> 1 AND pangkat_u = 'Premium', pangkat_u = 'Member', pangkat_u)

this code working -1 durasi_pre_u but not working change Premium to Member

i try to follow tutorial from this site https://www.w3resource.com/mysql/control-flow-functions/if-function.php

11
  • Do you want to update the table or just select the transformed data durasi_pre_u? Commented Sep 22, 2018 at 4:16
  • im edit my question Commented Sep 22, 2018 at 4:20
  • Should pangkat_u get updated after durasi_pre_u or before updating durasi_pre_u Commented Sep 22, 2018 at 4:27
  • i think after durasi_pre_u Commented Sep 22, 2018 at 4:30
  • 1
    You need to replace pangkat_u = 'Member' with 'Member' and change the <> to = Commented Sep 22, 2018 at 4:32

2 Answers 2

1

You have an error in your query, firstly you only want to change the value of pangkat_u when the current value of durasi_pre_u is 1 (so it is about to change to 0) so you need to change the <> to =. Secondly you have pangkat_u = 'Member' as the new value, which is treated as a boolean expression (i.e. a value of 1 or 0) by MySQL. What you actually want is just 'Member'. So your query should be:

UPDATE user 
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u), 
pangkat_u = IF(durasi_pre_u = 1 AND pangkat_u = 'Premium', 'Member', pangkat_u)
Sign up to request clarification or add additional context in comments.

Comments

0

In If function, you only need to specify the values to be set in case of true or false. Dont specify the field name = value (pangkat_u = 'Member')

Use the following :

UPDATE user 
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u), 
pangkat_u = IF(durasi_pre_u <> 1 AND pangkat_u = 'Premium', 'Member', pangkat_u)

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.