0

Given a query ending in where a and b and c or d, will this behave as where (a and b and c) or d, or where a and b and (c or d)? (Or something else?)

I've read the docs and they seem to suggest it would be the former as and has a higher precedence than or, but my results seem to performing the latter so I'm wondering if I'm either misunderstanding the docs or else I'm not generating the query correctly (via an ORM).

  • where a and b and c or d will this behave as where (a and b and c) or d. If you see something different, add the exact query – ypercubeᵀᴹ Sep 4 '19 at 15:07
  • 1
    In virtually all languages (a OR b AND c) does the AND first, equivalent to (a OR (b AND c)). When in doubt, use parens. – Rick James Sep 4 '19 at 17:46
2

If the query purpose is unclear, then you should make it clear.

You don't want to be relying on some relatively obscure ordering rules to get it right. You can't rely on them always staying the same throughout different versions of the same product, or if the underlying technology changes (e.g. Oracle -> MySQL).

If you want it to behave in one way, make it explicit how you want it to behave by using parentheses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.