How exactly does using OR in a MySQL statement differ with/without parentheses?

后端 未结 8 1602
难免孤独
难免孤独 2020-11-29 08:20

I have this query:

SELECT * FROM (`users`) WHERE `date_next_payment` <= \'2011-02-02\' 
    AND `status` = \'active\' OR `status` = \'past due\'
         


        
8条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 08:54

    You are changing the condition when you are adding the parentheses.

    In the initial case the result set consists of those records which satisfy the conditions

    `date_next_payment` <= '2011-02-02' AND `status` = 'active' 
    

    or the condition

     `status` = 'past due'.
    

    After you add the parentheses the result set will consist of those records which satisfy the condition

    `date_next_payment` <= '2011-02-02' 
    

    and any of the conditions

    `status` = 'active' 
    

    or

    `status` = 'past due'
    

提交回复
热议问题