Q:

Using nested case statement in MySQL

I have a query like so:
UPDATE image
SET angle  = CASE  
WHEN id = 1000 
THEN 90  
ELSE 
angle 
END WHERE 
id IN(1000) 

As you might have guessed, this SQL is built dynamically so that using a single statement I can update multiple records. What I want is that instead of updating an value directly as say 90 in this case, I want it to be incremental. So I guess I can do:
UPDATE image
SET angle  = CASE  
WHEN id = 1000 
THEN angle + 90    //<-- instead of 90, I use (angle + 90)
ELSE 
angle 
END WHERE 
id IN(1000) 

The only other thing I am trying to figure out is that if (angle + 90) >= 360, then I want the angle field to be 0. I am not sure on how to edit the SQL syntax to make this happen. Any pointers are greatly appreciated!

A:

You can use the modulus operator (%).  Whenever the expression angle + 90 exceeds 360 degrees, it will wrap back to zero.
UPDATE image
SET angle = CASE  
    WHEN id = 1000 
THEN (angle + 90) % 360
ELSE angle 
END
WHERE id IN(1000)

