简体   繁体   中英

operator precedence in mysql

哪个运算符: /*MySQL具有更高的优先级?

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.

The operators are prioritized as listed from the highest precedence to the lowest:

  1. BINARY, COLLATE
  2. NOT (logical negation), ! (logical negation)
  3. .- (unary minus), ~ (unary bit inversion)
  4. ^ (bitwise exclusive OR comparison)
  5. .* (multiplication), / (division), % (modulo)
  6. .- (subtraction), + (addition)
  7. << (bitwise shift left), >> (bitwise shift right)
  8. & (bitwise AND)
  9. | (bitwise OR)
  10. Comparison operators such as < >
  11. BETWEEN, NOT BETWEEN
  12. AND && (conjuction - logical addition)
  13. XOR (logical exclusive OR comparison)
  14. 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.

Source http://www.learn-mysql-tutorial.com/Expressions.cfm

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)

/ or * will have the highest priority in mysql.

They're of same priority.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM