skip to Main Content

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?

2

Answers


  1. 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'))
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search