Jump to content

UPDATE with CASE


sKunKbad

Recommended Posts

I'm trying to evaluation field A and update field A and B accordingly. If A = 0, then it should become 1. If A = 1, then B should become 1. It seems like it should be so simple, but it's not working:

 

UPDATE `the_table` 
SET `A` = CASE
		WHEN `A` = 0
		THEN `A` = 1
		ELSE `A`
	END,
`B` = CASE
		WHEN `A` = 1
		THEN `B` = 1
		ELSE `B`
	END
WHERE `ID` = 1

 

 

Link to comment
Share on other sites

You have left a number of things unsaid. Since this is in the MySql section I'm guessing you want to update a DB called 'the_table'. And that your using PHP to preform a SWITCH statement. I am also assuming that A and B are columns in that DB.

 

First thing you need to do is assign $A to the value of column A. (Do that your self).

 

Your PHP Code:

switch ($A) 
        case "0":
                UPDATE 'the_table' SET 'A' = 1;
                break;
        case "1":
                UPDATE 'the_table' SET 'B' = 1;
                break;      

:rtfm: Check the manual here:  http://php.net/manual/en/control-structures.switch.php         

Link to comment
Share on other sites

Actually, what I needed was if A = 0, then update A to 1. If A was already 1, then update B to 1.

 

This SQL statement ended up working:

 

UPDATE `the_table` 
SET `B` = CASE
		WHEN `A` = 1
		THEN 1
		ELSE `B`
	END,
`A` = CASE
		WHEN `A` = 0
		THEN '1'
		ELSE `A`
	END
WHERE `ID` = 1

 

I guess I didn't really say why it wasn't working, so nobody would have known what I was after. Thanks for your time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.