MySQL Select where a col1 (int) has the value 2 OR 3 AND col2 (tinybit) = 0

Think_Twice :

As you can see in the question I need to select all rows that have either of two values given dynamically and where the rows boolean (tinyint) is false

Here is my current query but it is only excluding the rows that have the category_company_id as 0 and category_disabled as 1 and not the ones with category_company_id as 1 and category_disabled as 1

SELECT * FROM `category` 
WHERE `category_company_id` = 1 
OR `category_company_id` = 0 
AND `category_disabled` = 0

What am I doing wrong?

Is there a way of separating the WHERE and OR from the AND?

Thanks in advance

forpas :

Your code is equivalent to:

WHERE `category_company_id` = 1 
OR (`category_company_id` = 0 AND `category_disabled` = 0)

because AND has higher priority than OR.
You can use parentheses:

WHERE (`category_company_id` = 1 OR `category_company_id` = 0 )
AND `category_disabled` = 0

or the operator IN:

WHERE `category_company_id` IN (0, 1) AND `category_disabled` = 0

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386376&siteId=1