BuildMyWeb Posted December 3, 2014 Share Posted December 3, 2014 Im having a hard time even explaining this so a search was not fruitful. I have an INT column in a mysql table. i want to UPDATE it in increments of 10 but the value should never exceed 100. what is an efficient way to do this? ie. if the current value in the field is 96, when it tried to update field = field+10 , it should dynamically only be field = field+4 so that we do not exceed the 100 mark. Quote Link to comment Share on other sites More sharing options...
Solution CroNiX Posted December 3, 2014 Solution Share Posted December 3, 2014 (edited) Using a MySQL IF() statement in your query. SELECT IF(field + x >= 100, 100, field + x) AS total Edited December 3, 2014 by CroNiX Quote Link to comment Share on other sites More sharing options...
BuildMyWeb Posted December 3, 2014 Author Share Posted December 3, 2014 thanks Cronix. for anyone who might find this useful, here is my query syntax: $query_ap = 'UPDATE ' . C_T_USERS . ' SET ap = IF(ap+8 >= 100, 100, ap+ WHERE status = "active" AND ap < 101'; Quote Link to comment Share on other sites More sharing options...
CroNiX Posted December 3, 2014 Share Posted December 3, 2014 Sure, but I don't get why you'd be incrementing by 8 or any other number if you said you were incrementing by 10? Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2014 Share Posted December 3, 2014 You could use SQL's LEAST() function also ... SET ap = ap + LEAST(100-ap, 10) ... so if ap is 93 then only 7 is added but if ap is <91 then 10 is added. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.