Jump to content

Need Help with Sql IF query


vincej

Recommended Posts

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 !

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/266838-need-help-with-sql-if-query/
Share on other sites

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

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"?

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'))

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.