How Can MySQL operator precedence affect result set?


MySQL follows operator precedence and it has the following list of operators, having the same precedence which is on the same line −

INTERVAL
BINARY, COLLATE
!
- (unary minus), ~ (unary bit inversion)
^
*, /, DIV, %, MOD
-, +
<<, >>
&
|
=, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
BETWEEN, CASE, WHEN, THEN, ELSE
NOT
&&, AND
XOR
||, OR
:=

For operators that occur at the same precedence level within an expression, evaluation proceeds from left to right.

Example

Following is an example which shows the effect of operator precedence on result set −

mysql> Select 5+3/2-2;
+---------+
| 5+3/2-2 |
+---------+
| 4.5000  |
+---------+
1 row in set (0.00 sec)

In the above evaluation, MySQL first evaluates /(div) because it is at higher precedence than +(Addition) or –(Subtraction).

Vikyath Ram
Vikyath Ram

A born rival

Updated on: 20-Jun-2020

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements