Home > Blockchain >  Why AND after a SET is not a syntax error in mysql?
Why AND after a SET is not a syntax error in mysql?

Time:12-30

I was surprised that this is not a syntax error:

UPDATE mytable  
SET deleted = 1 
AND name  = 'Cindy'

It doesn't affect the result of the command, is there any use of adding an AND after a SET? Or mysqsl will just ignore it?

CodePudding user response:

Because the expression:

1 AND name  = 'Cindy'

is a Boolean expression that evaluates to 0 for False or 1 for True and the value of this expression will be assigned to the column deleted, like:

SET deleted = (1 AND (name = 'Cindy'))

CodePudding user response:

The statement takes into account the operators precedence. And the query acts as

UPDATE mytable  
SET deleted = (1 AND (name  = 'Cindy'))

I.e. firstly the condition name = 'Cindy' is tested producing TRUE (1), FALSE (0) or NULL.

Then the expression 1 AND {result} is evaluated.

And finally the result of this expression evaluation is assigned into deleted column.

DEMO fiddle

If you receive the same result like for

UPDATE mytable  
SET deleted = 1 

then name column value is equal to 'Cindy' using current collation.

  • Related