2

Which operator: / or * will have the higher priority in MySQL?

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

yes it was wrong Pax =) In MySQL its evaluated in the order of usage when they are on the same level. I was reading up on Transact-SQL here: msdn.microsoft.com/en-us/library/ms190276.aspx which is not the same for MySQL =) Thank you PAx
1

They are the same see http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html

Comments

1

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)

Comments

0

/ or * will have the highest priority in mysql.

They're of same priority.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.