8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

14.4.1 运算符优先级14.4.1 Operator Precedence

运算符优先级如下所示,从最高优先级到最低优先级。同一行显示的运算符具有相同的优先级。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 logical OR operator. With PIPES_AS_CONCAT enabled, || is string concatenation, with a precedence between ^ and the unary operators.

  • 默认情况下,! 的优先级高于 NOT。启用 HIGH_NOT_PRECEDENCE 后,! 和 NOT 的优先级相同。By default, ! has a higher precedence than NOT. With HIGH_NOT_PRECEDENCE enabled, ! and NOT have 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