OR WHERE and AND in mySQL Query

前端 未结 2 1572
面向向阳花
面向向阳花 2020-12-17 18:57

Basically, I\'m trying to get data from an SQL based on two different groups, t.type has to equal single and t.status has to equal

相关标签:
2条回答
  • 2020-12-17 19:36

    You can use the IN condition:

    WHERE 
        t.type = 'single' AND t.status = '1' AND t.org IN ('RA','DUAL')
    

    Or you can use brackets to group conditions:

    WHERE
        t.type = 'single' AND t.status = '1' AND (t.org = 'RA' OR t.org = 'DUAL')
    
    0 讨论(0)
  • 2020-12-17 19:43

    AND has higher precedence than OR, so your existing expression is currently evaluated as:

    WHERE 
        (t.type = 'single' AND t.status='1' AND t.org = 'RA') OR t.org = 'DUAL'
    

    To force alternative logic, one needs to include explicit parentheses:

    WHERE 
        t.type = 'single' AND t.status='1' AND (t.org = 'RA' OR t.org = 'DUAL')
    

    However, in this case, one can use MySQL's IN() operator instead of OR:

    WHERE 
        t.type = 'single' AND t.status='1' AND t.org IN ('RA','DUAL')
    
    0 讨论(0)
提交回复
热议问题