Question

In MySQL, NOT and ! do the same thing (Logical Not), but they are at different operator precedences. ! appears above arithmetic and equality operators, NOT is below those but above logical operators like AND and OR. See http://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html

In MySQL 3 and 4 they shared the same precedence. Why was this change made?

Was it helpful?

Solution

Given that a flag HIGH_NOT_PRECEDENCE was introduced to get the old behaviour back, I think that part of the documentation explains it best:

The precedence of the NOT operator is such that expressions such as NOT a BETWEEN b AND c are parsed as NOT (a BETWEEN b AND c). In some older versions of MySQL, the expression was parsed as (NOT a) BETWEEN b AND c.

In other words, NOT did not behave as many people thought it did and therefore the precedence was changed. You're less likely to trip over this when writing !a as that more intuitively expresses that you're negating a.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top