I have an SQL query to update the rotational offset of a photo. The query should add 1 to the value, unless the value is 3, in which case it should set it back to 0 (as in, 4 lots of 90 degrees rotation is the same as no rotation at all).
I’m not sure if what’s below is the most efficient way to do it, and I’d appreciate suggestions of simpler solutions that don’t require me to list out the mappings like I have.
However, my question is, why do I get a MySQL syntax error? When I use the SELECT subquery on it’s own, it returns the result as expected.
UPDATE `photos` p SET p.rotational_offset=map.new_value INNER JOIN (
SELECT 0 AS rotational_offset, 3 AS new_value
UNION SELECT 1 , 0
UNION SELECT 2 , 1
UNION SELECT 3 , 2
) map ON p.rotational_offset=map.rotational_offset WHERE p.photo_id="22";
First, a much simpler solution is to use the
MODoperator (%):See it working online: sqlfiddle
And now to actually answer your question… You get an error because you aren’t using the correct syntax for a multiple table update. The table references come first, before the
SETclause. The correct syntax is as follows:Your statement doesn’t work because you put the inner join after the
SETclause. A corrected version is as follows: