If you are like me, no matter how often you use AND/OR logic, it will ALWAYS trip you up, then man have I got a cool little ‘trick’ for you!
The theory behind it is that each logic statement is a boolean (returning true or false) and turning the whole complex logic into a simple math equation.
The math equation would be that each ‘AND’ would become multiplication and each ‘OR’ would become addition. You then fill in the 1 (true) or 0 (false) accordingly into each of your statements to create the math equation.
From there, you just add/multiple to get your result. If the result is greater than 0 then it has passed and will be included in the filter, but if it equals 0, then it has failed and will be filtered out.
Example: (via SQL)
WHERE a.Field1 IN ('option1','option2','option3')
AND (a.Field2 != 'option4' OR a.Field3 = 'option5')
Using the assumption that:
Field1 is ‘option1’
Field2 is ‘option4’
Field3 is ‘option6’
This would be translated to:
1 * (0+0) = 0
Which means that it would fail the filter and be excluded.
Now if we were to change Field2 to be ‘option2’, this would then make the second part (1+0)
giving an equation of 1*1
, which would provide a result above 0 and would pass.
I hope this helps you as much as it has helped me!