Which operator: /
or *
will have the higher priority in MySQL
?
views:
371answers:
6/ or * will have the highest priority in mysql.
They're of same priority.
They are the same see http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html
Like most programming languages, mysql performs /
and *
(as well as DIV, % and MOD) from left to right, in the order they occur. For instance:
1 / 2 * 3 / 4 / 5 * 6
is equivalent to
((((1 / 2) * 3) / 4) / 5) * 6
See http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html for a complete list of operator precedence.
They have the same precedence, * is listed first, and MySQL will evaluate left to right.
http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html
My advice is use brackets () to make things clear.
It's always best to check, and that can be done very easily with MySQL:
mysql> SELECT 2*3/2, 2/3*2, 2*(3/2) ;
+--------+--------+---------+
| 2*3/2 | 2/3*2 | 2*(3/2) |
+--------+--------+---------+
| 3.0000 | 1.3333 | 3.0000 |
+--------+--------+---------+
1 row in set (0.00 sec)
The operators are prioritized as listed from the highest precedence to the lowest:
- BINARY, COLLATE
- NOT (logical negation), ! (logical negation)
- .- (unary minus), ~ (unary bit inversion)
- ^ (bitwise exclusive OR comparison)
- .* (multiplication), / (division), % (modulo)
- .- (subtraction), + (addition)
- << (bitwise shift left), >> (bitwise shift right)
- & (bitwise AND)
- | (bitwise OR)
- Comparison operators such as < >
- BETWEEN, NOT BETWEEN
- AND && (conjuction - logical addition)
- XOR (logical exclusive OR comparison)
- OR || (disjunction - either/or comparison)
As per list, the comparison BINARY operator has precedence over the BETWEEN and ampersand (&) operators. Operators on the same line above have the same level of precedence, and are evaluated in the order of usage.
The answer is: it shouldn't matter. If you have an expression that you are not sure how it's evaluated, use parens. They're free.