MySQL: execute an "UPDATE" only if an specific row exists I want to execute an UPDATE clause only in the case a specific row exists. Specifically, if a row in gallery_image exists with a specified position, I want to update all rows with a position higher than that value, flipping the sign of the position value.

So this is my try:

IF SELECT * FROM gallery_image WHERE position = 'X' 
UPDATE gallery_image SET position = position * -1


but I get an error.

EDIT: After the first comment after my question I have tried this:

update gallery_image if ( EXISTS ( select * from gallery_image position = 8 )) set position = position * -1


but it says 


  An alias was found before. (near "(" at position 24)"
The IF statement can only be used inside Stored Routines.  This explains the syntax error.

Are you trying to change all rows in the table if a certain row exists?

It's a bit clumsy, but here is a purely SQL way:

SET @x := EXISTS ( SELECT * FROM gallery_image WHERE position = 8 );
UPDATE gallery_image
    SET position = - position
    WHERE @x;


(It is not normal to change all rows of a table.  What is the application doing?  Can it be achieved without such a massive query?)

If you only want to change the one row with 8:

UPDATE gallery_image
    SET position = - position
    WHERE position = 8;