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
https://forums.phpfreaks.com/topic/240349-update-with-case/
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
https://forums.phpfreaks.com/topic/240349-update-with-case/#findComment-1234767
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
https://forums.phpfreaks.com/topic/240349-update-with-case/#findComment-1234768
Share on other sites

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.