SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using CASE within UPDATE

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

    Code:
    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
    What am I doing wrong?

  2. #2
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out:

    Code:
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •