运算符优先级如下所示,从最高优先级到最低优先级。同一行显示的运算符具有相同的优先级。Operator precedences are shown in the following list, from highest precedence to the lowest. Operators that are shown together on a line have the same precedence.
INTERVAL BINARY, COLLATE ! - (unary minus), ~ (unary bit inversion) ^ *, /, DIV, %, MOD -, + <<, >> & | = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN, MEMBER OF BETWEEN, CASE, WHEN, THEN, ELSE NOT AND, && XOR OR, || = (assignment), :=
的优先级=取决于它是作为比较运算符(=)还是作为赋值运算符(=)使用。当用作比较运算符时,它的优先级与<=>,
>=,
>,
<=,
<,
<>,
!=,
IS,
LIKE,
REGEXP,以及IN()相同。用作赋值运算符时,它的优先级与:=.
第 15.7.6.1 节,“变量赋值的 SET 语法”,以及第 11.4 节,“用户定义变量”, explain how MySQL determines
which interpretation of = should apply.The precedence of = depends on whether it is
used as a comparison operator
(=) or as an
assignment operator
(=). When
used as a comparison operator, it has the same precedence as
<=>,
>=,
>,
<=,
<,
<>,
!=,
IS,
LIKE,
REGEXP, and
IN(). When used as an assignment
operator, it has the same precedence as
:=.
Section 15.7.6.1, “SET Syntax for Variable Assignment”, and
Section 11.4, “User-Defined Variables”, explain how MySQL determines
which interpretation of = should apply.
对于在表达式中具有相同优先级的运算符,计算按从左到右的顺序进行,唯一的例外是赋值运算符从右到左计算。For operators that occur at the same precedence level within an expression, evaluation proceeds left to right, with the exception that assignments evaluate right to left.
某些运算符的优先级和含义取决于 SQL 模式:The precedence and meaning of some operators depends on the SQL mode:
默认情况下,
||是逻辑OR运算符。启用PIPES_AS_CONCAT后,||用于字符串连接,其优先级介于^与一元运算符之间。By default,||is a logicalORoperator. WithPIPES_AS_CONCATenabled,||is string concatenation, with a precedence between^and the unary operators.默认情况下,
!的优先级高于NOT。启用HIGH_NOT_PRECEDENCE后,!和NOT的优先级相同。By default,!has a higher precedence thanNOT. WithHIGH_NOT_PRECEDENCEenabled,!andNOThave the same precedence.
参见 第 7.1.11 节,“服务器 SQL 模式”。See Section 7.1.11, “Server SQL Modes”.
运算符的优先级决定了表达式中各项的计算顺序。要覆盖此顺序并明确地分组各项,请使用括号。例如:The precedence of operators determines the order of evaluation of terms in an expression. To override this order and group terms explicitly, use parentheses. For example:
mysql>SELECT 1+2*3;-> 7 mysql>SELECT (1+2)*3;-> 9