vincej Posted August 9, 2012 Share Posted August 9, 2012 HI - So far I have a query that is not yet complete, but what I have works: UPDATE `referals` SET `code` = `code` + 1, `claiment` = $email WHERE (customerid=$customerid or `referedemail` = '$referedemail') Now I want to add a conditional clause - this is where I need help as I am failing miserably. If the claimant column already contains the email address being offered in the variable $email then do not update. If it does not contain the email then update. I'm clueless how to get this done, Many Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/266838-need-help-with-sql-if-query/ Share on other sites More sharing options...
requinix Posted August 9, 2012 Share Posted August 9, 2012 And you want to use an IF? Why? WHERE (customerid=$customerid or `referedemail` = '$referedemail') AND `claiment` != $email Quote Link to comment https://forums.phpfreaks.com/topic/266838-need-help-with-sql-if-query/#findComment-1368003 Share on other sites More sharing options...
Mahngiel Posted August 9, 2012 Share Posted August 9, 2012 If the claimant column already contains the email address being offered in the variable $email then do not update. If it does not contain the email then update. If you do not have an entry for a user with an email address, you'll be doing an INSERT, not an UPDATE. So your PHP logic will have to check for the existence of the email first, THEN update or insert accordingly. But honestly, it's hard to tell what you're doing with no relevant data Quote Link to comment https://forums.phpfreaks.com/topic/266838-need-help-with-sql-if-query/#findComment-1368005 Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 I think you are trying to solve a problem that doesn't exist. MySql will not perform an update if the data would remain unchanged after the update. And even if it did, what would be catastrophic in changing "abc@xyz" to "abc@xyz"? Quote Link to comment https://forums.phpfreaks.com/topic/266838-need-help-with-sql-if-query/#findComment-1368079 Share on other sites More sharing options...
The Little Guy Posted August 9, 2012 Share Posted August 9, 2012 Don't forget to put quotes around $email Quote Link to comment https://forums.phpfreaks.com/topic/266838-need-help-with-sql-if-query/#findComment-1368168 Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 or did you mean the entire update? UPDATE `referals` SET `code` = CASE WHEN `claiment` = '$email' THEN `code` ELSE `code` + 1 END, `claiment` = CASE WHEN `claiment` = '$email' THEN `claiment` ELSE '$email' END WHERE ((customerid = $customerid) or (`referedemail` = '$referedemail')) Quote Link to comment https://forums.phpfreaks.com/topic/266838-need-help-with-sql-if-query/#findComment-1368173 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.