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

后端 未结 8 1615
难免孤独
难免孤独 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 09:16

    It's because when you are not using the parentheses you actually saying :

    `date_next_payment` <= '2011-02-02'     AND `status` = 'active'
    OR
    `status` = 'past due'.
    

    Which means that when status` = 'past due' this record would be shown too. regarless of passing the other conditions.

    But when you do use parentheses you require:

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

    AND to pass one of the two other conditions.

提交回复
热议问题