Beauford Posted November 2, 2008 Share Posted November 2, 2008 I posted this in the MySQL group, but it might be relevent here as well. If not, I apologize for the post. I am trying to add interest to a dollar value, but I don't want the interest to go higher than a certain number. Example (update table set SET bankmoney = IF (bankmoney * .02 < 1000, bankmoney=bankmoney * .02 else bankmoney = bankmoney + 1000); Of course the above doesn't work, The question is, how do I get it to work. Thanks. I only have one gray hair left and I'd like to keep it. B Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/ Share on other sites More sharing options...
corbin Posted November 2, 2008 Share Posted November 2, 2008 I would just do it in 2 queries.... Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-680792 Share on other sites More sharing options...
Beauford Posted November 2, 2008 Author Share Posted November 2, 2008 Can you elaborate. I am going to be checking a DB with 30000+ entries and each one has to be checked. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-680796 Share on other sites More sharing options...
corbin Posted November 3, 2008 Share Posted November 3, 2008 UPDATE table SET bankmoney = bankmoney*.02 WHERE bankmoney < 50000; UPDATE table SET bankmoney = bankmoney+1000 WHERE bankmoney >= 50000; Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-680929 Share on other sites More sharing options...
fenway Posted November 3, 2008 Share Posted November 3, 2008 Or with a CASE statement: UPDATE yourTable SET bankmoney = CASE WHEN bankmoney >= 50000 THEN bankmoney*.02 ELSE bankmoney+1000 END Assuming NULL isn't an option. Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-681455 Share on other sites More sharing options...
Beauford Posted November 5, 2008 Author Share Posted November 5, 2008 That's not what I need. I need to find out what the interest is first, and if it is greater than a certain value, only update it by that value. So if the interest is 1000 and the ceiling is 500, then I want to update bankmoney by 500. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-682735 Share on other sites More sharing options...
fenway Posted November 6, 2008 Share Posted November 6, 2008 well, then what you originally posted should work, with minor syntactical changes: update table set SET bankmoney = IF (bankmoney * .02 < 1000, bankmoney * .02, bankmoney + 1000) (untested -- backup your table first) Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-683730 Share on other sites More sharing options...
Beauford Posted November 8, 2008 Author Share Posted November 8, 2008 I actually figured it out using a where statement and it works perfectly. I just had to use two statements. Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-684953 Share on other sites More sharing options...
fenway Posted November 8, 2008 Share Posted November 8, 2008 I actually figured it out using a where statement and it works perfectly. I just had to use two statements. Then post those two. Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-685116 Share on other sites More sharing options...
Beauford Posted November 8, 2008 Author Share Posted November 8, 2008 I actually figured it out using a where statement and it works perfectly. I just had to use two statements. Then post those two. $db->query("UPDATE users set bankmoney = bankmoney+bankmoney * .02 where (bankmoney > 0 and bankmoney * .02 < 300000) and donatordays = 0"); $db->query("UPDATE users set bankmoney = bankmoney+300000 where (bankmoney > 0 and bankmoney * .02 > 300000) and donatordays = 0"); Quote Link to comment https://forums.phpfreaks.com/topic/131118-solved-if-statements-in-mysql-update/#findComment-685267 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.